[英]Execute Multiple DML on single java.sql.Statement
How transactions will behave; 交易行为如何; If I use following syntax to execute multiple DML using single java.sql.Statement
如果我使用以下语法使用单个java.sql.Statement
执行多个DML
String sqlStr = "INSERT INTO.... \n update t1 set....";
statemet.execute(sqlStr);
I am not altering default connection setting (autocommit = true). 我没有更改默认的连接设置(autocommit = true)。
I am aware this is not the right way / best practice; 我知道这不是正确的方法/最佳做法; just curious to know the transactional nature of above code That is if update fails with SQLException, will insert rollback? 只是好奇地知道上述代码的事务性质,即如果SQLException更新失败,是否将插入回滚?
With autocommit
set to true, then the individual statements would very likely execute in their own individual transactions. 在autocommit
设置为true的情况下,单个语句很可能会在其自己的单个事务中执行。 This might depends on the database, but I suspect not. 这可能取决于数据库,但我怀疑不是。
Best way to find out is to try it. 找出答案的最佳方法是尝试一下。
I tried in MySQL environment. 我在MySQL环境中尝试过。 It worked very well. 效果很好。 And I think it is the basic way to do so. 我认为这是这样做的基本方法。
Tried out, got some surprising results with MS SQL sever 2008 尝试使用MS SQL Sever 2008获得了一些令人惊讶的结果
I tried 2 update statements and as per observation they run in a single transaction; 我尝试了2条更新语句,根据观察,它们在单个事务中运行; that is if second update fails first update statement is rolled back. 也就是说,如果第二个更新失败,则第一个更新语句将回滚。
The surprising thing I noticed is : 我注意到的令人惊讶的是:
https://connect.microsoft.com/SQLServer/feedback/details/620522/invalid-object-exception-not-thrown-by-com-microsoft-sqlserver-jdbc-sqlserverdriver# https://connect.microsoft.com/SQLServer/feedback/details/620522/invalid-object-exception-not-thrown-by-com-microsoft-sqlserver-jdbc-sqlserverdriver#
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.