Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL PreparedStatement & autocommit

If I create a preparedstatement using a JDBC connection which has "autocommit off", do I need to call commit() to make the transaction permanent or only the prepare call is enough? Note that I only want to prepare a statement and keep it for later execution.

Many thanks!

like image 981
AliM Avatar asked May 30 '12 10:05

AliM


1 Answers

The answer is: yes you have to call commit.

The "prepare" step does not do anything that is relevant for transaction handling on the server.

You have to call executeUpdate() (or execute()) to start a transaction (assuming the used SQL does in fact start a transaction)

Using a PreparedStatement without auto-commit requires the following steps:

  1. Prepare the statement
  2. Set the parameter values
  3. call executeUpdate() (or execute() depending on the type of statement)
  4. call Connection.commit()

To make things clearer: calling prepareStatement() does not require a commit().

like image 71
a_horse_with_no_name Avatar answered Nov 05 '22 03:11

a_horse_with_no_name