【发布时间】:2019-01-02 07:27:06
【问题描述】:
我想知道是否可以在一个事务中创建两个不同的连接,所以假设有这个代码(我在表“employe”中执行创建操作并在表“log”中创建日志):
try {
InitialContext initialContext = new InitialContext();
DataSource datasource = (DataSource) initialContext.lookup("java:/comp/env/jdbc/postgres");
Connection connection_db= datasource.getConnection();
PreparateStatement p1 //Preparate statement to put the employe parameter
connessione_db.setAutoCommit(false);
connessione_db.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
p1.execute();
//This create another connect
LOGStatic.createLog("CREATE EMPLOYE");
connessione_db.commit();
connessione_db.setAutoCommit(true);
}catch(....){
connection_db.rollback();
}
这是LOGStatic.createLog("CREATE EMPLOYE");的方法
public static void creaLogException(String messaggio) {
Connection connection_db= null;
InitialContext initialContext;
try {
initialContext = new InitialContext();
DataSource datasource = (DataSource) initialContext.lookup("java:/comp/env/jdbc/postgres");
connection_db= datasource.getConnection();
// the code continues with the save operation
} catch (NamingException n) {
}
// actual jndi name is "jdbc/postgres"
catch (SQLException s) {
}
我的问题是否可能是这种行为并且提交和回滚操作没有问题,或者不可能有另一个连接? 任何人都可以提供帮助
【问题讨论】:
标签: java jdbc transactions