假设我想在两个不同的表中保存删除两个元素,所以我必须创建事务。我的问题是我必须在哪里放置 PreparedStatement
元素?我必须这样写(我的意思是在交易中)
connection_database.setAutoCommit(false);
connection_database.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
PreparedStatement p1.......
PreparedStatement p2;
p1.execute();
p2.execute();
connessione_db.commit();
connessione_db.setAutoCommit(true);
或者我可以这样说吗?
PreparedStatement p1.......
PreparedStatement p2;
connection_database.setAutoCommit(false);
connection_database.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
p1.execute();
p2.execute();
connessione_db.commit();
connessione_db.setAutoCommit(true);
- 第一种创建
PreparedStatement
的方法是在事务中 - 创建
PreparedStatement
的第二种方式是在事务之外。
有人可以帮助我吗?
最佳答案
在符合规范的 JDBC 实现中,您可以使用任一顺序:准备好的语句的生命周期比事务长,并且在事务提交后应该可以重用。
但是,根据驱动程序和数据库系统,在准备语句之前首先禁用自动提交可能会更有效。这可能取决于驱动程序在准备报表时是否需要事务。如果驱动程序在准备时需要事务,则在准备后禁用自动提交可能会在准备这两个语句的过程中启动(并提交)一两个事务,但如果在禁用自动提交后准备它们,则准备可以使用与随后用于实际执行的事务相同的事务。
关于java - 我必须在交易中放置准备好的语句的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51514579/