2

I would like to know what is the autocommit mode by default in JPA EntityManager with EclipseLink, is it autocommit-true or false? If by default it is false, when it is set to true, during the commit/rollback? ie., during entitymanager.getTranaction.commit() / entitymanager.getTranaction.rollback()?

And finally how to find out the autocommit mode programatically in JPA Eclipselink? Thnaks!

Faz
  • 534
  • 1
  • 9
  • 27

1 Answers1

1

If you referring to auto commit on the JDBC connection, EclipseLink sets it to false when starting a transaction and true after commit/rollback, unless a JTA transaction controller is used. Checking the auto commit setting requires getting the JDBC connection from JPA, see http://wiki.eclipse.org/EclipseLink/FAQ/JPA#How_to_access_the_JDBC_connection.3F

Chris
  • 20,138
  • 2
  • 29
  • 43
  • Thanks, isn't it the otherway around. i.e, Setting the autocommit to false when starting a tran and the setting it back to true after commit/rollback. Because, by default the JDBC will have the autocommit set to true. I tried the sample as you mentioned and when I check the con.getAutoCommit(), it shows as false. So I was wondering thatthe Eclipse link will always have the autocmoit mode set to false. Please correct me if am wrong. – Faz May 27 '14 at 17:21
  • I tried the below code (as in the link you provided ) and procured that the autocommit was set to **false** `em.getTransaction().begin(); ..... final Accessor accessor; if (....) { .... } else { unitOfWork.beginEarlyTransaction(); ....} java.sql.Connection connection = accessor.getConnection(); log.info("Default connection:" + connection.getTransactionIsolation()); log.info("Default getAutoCommit:" + connection.getAutoCommit()); em.getTransaction().commit();` – Faz May 27 '14 at 17:29
  • Yes, EL sets it to false when it needs to begin a transaction. I've corrected the answer. – Chris May 27 '14 at 17:59
  • Okie, so that means that we dont have to explicitly set the autocommit to true. However,we have some issues wherein even after the the transaction is committed. The data is not persisted. (This is in a clustered environment). – Faz May 27 '14 at 18:03
  • What data? You shouldn't need to look at connection auto commit settings if using JPA. – Chris May 27 '14 at 18:14
  • True, but some how the action that we perform is not commited . say for instance, a insert into a table doesn't gets commited. Meanwhile when another thread is trying to access the same data (insert/delete), there a lock in the db. which concludes that the transaction is not getting commited. – Faz May 27 '14 at 18:23
  • 1
    How are you inserting? Turn on Eclipselink logging to track what might be happening. – Chris May 28 '14 at 14:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54642/discussion-between-faz-and-chris). – Faz May 28 '14 at 14:26
  • I kinda identified the issue, looks like when there is an exception with Insert/Update, EL issues a SELECT 1 which actually is holding the lock and connection. Please see below, – Faz May 31 '14 at 21:35
  • below is the error log, **Thread(Thread[Timer-0,10,main])--INSERT INTO XXXX Thread(Thread[Timer-0,10,main])--bind => [X, X] Thread(Thread[Timer-0,10,main])--End Batch Statements Thread(Thread[Timer-0,10,main])--SELECT 1 ClientSession(1803685687)--Thread(Thread[Timer-0,10,main])--Local Exception Stack: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1.2.v20101206-r8635): org...DatabaseException Internal Exception: com..SybBatchUpdateException: JZ0BE: BatchUpdateException: Error occurred while executing batch statement: Attempt to insert duplicate key row in XXXX** – Faz May 31 '14 at 21:41
  • What help do you need? You are getting a batch update exception from inserting a duplicate entry. You should focus on the actual problem in your questions, as this new issue seems completely unrelated to the original question that dealt with autocommit. Maybe start a new question with exactly what you are seeing and trying to fix. – Chris Jun 02 '14 at 13:52