0

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?

arjuncc
3,2975 gold badges46 silver badges79 bronze badges
asked Nov 9, 2010 at 18:24

3 Answers 3

1

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.

answered Nov 9, 2010 at 23:37
Sign up to request clarification or add additional context in comments.

Comments

0

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#

answered Nov 10, 2010 at 11:01

1 Comment

In this sort of situation, you can make SQL Server roll back previous statements (or not) by using SET XACT_ABORT: msdn.microsoft.com/en-us/library/ms188792.aspx
0

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

answered Oct 12, 2013 at 17:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.