Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql transaction handling with java

I have two blocks of queries with preparedStatement.

This is the first:

String sql = "update cikan_malzeme set miktar = ? where proje_id = ? and malzeme_id = ?";
PreparedStatement prep = dbConnect.connection.prepareStatement(sql);
prep.setFloat(1, toplam);
prep.setInt(2, pid);
prep.setInt(3, mid);
prep.executeUpdate();

And this is the second:

String sql2 = "update malzemeler set miktar = ? where malz_adi = ?";
PreparedStatement prep2 = dbConnect.connection.prepareStatement(sql2);
prep2.setFloat(1, fark);
prep2.setString(2, malzemeadi);
prep2.executeUpdate();

Now I want to execute them with the transaction BEGIN; and COMMIT; How can I handle transaction with preparedStatement?

like image 238
Zzap Avatar asked Nov 23 '11 09:11

Zzap


1 Answers

You should use Connection.setAutoCommit(false) to disable auto-commit and Connection.commit() and Connection.rollback().

When auto-commit is disabled, a transaction will be started automatically the first time you execute a command or query that requires a transaction.

You should not be using the database specific transaction control commands, as the driver will most likely do additional cleanup of resources when a commit() or rollback() is issued.

like image 71
Mark Rotteveel Avatar answered Oct 08 '22 00:10

Mark Rotteveel