Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable autocommit in sqlite4java?

Tags:

java

sqlite

I have recently been playing with the sqlite4java library. I think I have it pretty much figured out. The only think that bothers me is that I do not know how to switch off the auto-commit using this library. Can anyone please help? A code example would be much appreciated.

Thanks in advance, Boro

like image 545
Boro Avatar asked Feb 14 '11 23:02

Boro


People also ask

How do I turn off auto commit?

To disable autocommit mode explicitly, use the following statement: SET autocommit=0; After disabling autocommit mode by setting the autocommit variable to zero, changes to transaction-safe tables (such as those for InnoDB or NDB ) are not made permanent immediately.

How do I turn off autocommit in Java?

However, you can disable auto-commit mode with the setAutoCommit() method of the connection object (either java. sql. Conection or oracle.

How do you check autocommit is on or off?

To tell if AUTOCOMMIT is on or off, issue the set command: $ \set ... AUTOCOMMIT = 'off' ...


3 Answers

Jefromi and king_nak are correct - you just need to issue SQL statements that begin and end a transaction.

SQLiteConnection con = new SQLiteConnection();
con.exec("BEGIN");
// do transaction work - auto-commit is disabled
con.exec("COMMIT");
// auto-commit is enabled again
like image 103
sereda Avatar answered Oct 21 '22 06:10

sereda


Edit: I confused sqlite4java with the sqliteJDBC package. So the below code will not help. I'm keeping it for reference nevertheless.

After you obtained the connection, just call setAutoCommit(false)

java.sql.Connection con = DriverManager.getConnection("jdbc:sqlite:/your/db/file", "user", "password");
con.setAutoCommit(false);
like image 21
a_horse_with_no_name Avatar answered Oct 21 '22 06:10

a_horse_with_no_name


Referring to SQLite's C interface description:

The sqlite3_get_autocommit() interface returns non-zero or zero if the given database connection is or is not in autocommit mode, respectively. Autocommit mode is on by default. Autocommit mode is disabled by a BEGIN statement. Autocommit mode is re-enabled by a COMMIT or ROLLBACK.

So you disable auto-commit by a BEGIN TRANSACTION statement. No separate API function is present for this

like image 1
king_nak Avatar answered Oct 21 '22 07:10

king_nak