Blob
Blob

Reputation: 1

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?

Upvotes: 0

Views: 1877

Answers (3)

Jaydeep_Ranipa
Jaydeep_Ranipa

Reputation: 11

I tried in MySQL environment. It worked very well. And I think it is the basic way to do so.

Upvotes: 0

Dhananjay
Dhananjay

Reputation: 3965

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#

Upvotes: 0

skaffman
skaffman

Reputation: 403451

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.

Upvotes: 1

Related Questions