bitronix.tm.configuration=bitronix-config.properties bitronix.tm.journal.disk.logPart1Filename=/tmp/btm1.tlog bitronix.tm.journal.disk.logPart2Filename=/tmp/btm2.tlog bitronix.tm.resource.configuration=/path/to/resources.properties resource.ds1.className=org.mysql.Driver resource.ds1.url=jdbc:mysql://localhost:3306/mydb resource.ds1.user=username resource.ds1.password=password import bitronix.tm.BitronixTransactionManager; import bitronix.tm.TransactionManagerServices; import bitronix.tm.TransactionManagerServices.TransactionStatus; import bitronix.tm.resource.jdbc.PoolingDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class ConcurrentTransactionExample { public static void main(String[] args) throws Exception { TransactionManagerServices.getConfiguration().setDisableJmx(true); TransactionManagerServices.getTransactionManager(); PoolingDataSource dataSource = new PoolingDataSource(); dataSource.setClassName("org.mysql.Driver"); dataSource.setUniqueName("ds1"); dataSource.setMinPoolSize(1); dataSource.setMaxPoolSize(5); dataSource.setAllowLocalTransactions(true); dataSource.getDriverProperties().put("user", "username"); dataSource.getDriverProperties().put("password", "password"); dataSource.getDriverProperties().put("URL", "jdbc:mysql://localhost:3306/mydb"); Connection connection = dataSource.getConnection(); BitronixTransactionManager transactionManager = TransactionManagerServices.getTransactionManager(); transactionManager.begin(); try { PreparedStatement statement = connection.prepareStatement("INSERT INTO my_table VALUES (?)"); statement.setString(1, "value1"); statement.executeUpdate(); // perform other database operations transactionManager.commit(); } catch (SQLException e) { transactionManager.rollback(); e.printStackTrace(); } finally { connection.close(); dataSource.close(); transactionManager.shutdown(); } } }


上一篇:
下一篇:
切换中文