简体   繁体   中英

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

String sqlStr = "INSERT INTO.... \n update t1 set....";

statemet.execute(sqlStr);

I am not altering default connection setting (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?

With autocommit set to true, then the individual statements would very likely execute in their own individual transactions. This might depends on the database, but I suspect not.

Best way to find out is to try it.

I tried in MySQL environment. 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

I tried 2 update statements and as per observation they run in a single transaction; 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#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM