Community
Participate
Working Groups
Build Identifier: 2.1.0 I'm getting a "No transaction is currently active" error when attempting an update native query and opening a transaction right before the query. Reproducible: Always Steps to Reproduce: 1. Run the main class in the attached project (Netbeans project) or double click the jar attached
I'm unable to attach the files. When I click submit (in the add attachment form) the path to the file clears and nothing happens.
Javier, If you are not missing the begin() transaction start as stated, then maybe you are starting a transaction on another EntityManager's transaction? The code below works and can be broken by doing this. Paste your stacktrace, some of your FINEST logs and a code portion and we can have a look at triaging your issue. /michael <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/> <property name="eclipselink.target-database" value="Derby"/> >1) Native query working below on Derby in SE (no EM or EMF injection - Persistence bootstrap only) private void nativeQuery() { Query aQuery = null; int version = 3; StringBuffer aBuffer = new StringBuffer(); aBuffer.append("UPDATE DPAR_SCPROCUNIT SET STATE = 1, PENDINGSTATE = 1, ALU_VERSION = "); aBuffer.append(version + 1); aBuffer.append(" WHERE ((PROCUNIT_ID = 47) AND (ALU_VERSION = "); aBuffer.append(version); aBuffer.append(") AND (PROCUNIT_ID IN "); aBuffer.append("(SELECT PROCUNIT_ID FROM DPAR_SCPROCUNIT)))");//#DPAR_SCPROCUNIT)))"); try { aQuery = getEntityManager().createNativeQuery(aBuffer.toString()); // Convert to delegate when on EE container - otherwise just return same EE EM setEntityManager(JpaHelper.getEntityManager(getEntityManager())); // If container managed TX - do not begin/end as SE if(JpaHelper.isEclipseLink(entityManagerFactory)) { getEntityManager().getTransaction().begin(); } aQuery.executeUpdate(); if(JpaHelper.isEclipseLink(entityManagerFactory)) { getEntityManager().getTransaction().commit(); } } catch (Exception e) { e.printStackTrace(); } } [EL Fine]: 2010-09-15 17:56:16.757--ClientSession(20738936)--Connection(213274)--Thread(Thread[main,5,main])--UPDATE DPAR_SCPROCUNIT SET STATE = 1, PENDINGSTATE = 1, ALU_VERSION = 4 WHERE ((PROCUNIT_ID = 47) AND (ALU_VERSION = 3) AND (PROCUNIT_ID IN (SELECT PROCUNIT_ID FROM DPAR_SCPROCUNIT))) [EL Finer]: 2010-09-15 17:56:16.772--UnitOfWork(902782)--Thread(Thread[main,5,main])--begin unit of work commit [EL Finer]: 2010-09-15 17:56:16.772--ClientSession(20738936)--Connection(213274)--Thread(Thread[main,5,main])--commit transaction [EL Finer]: 2010-09-15 17:56:16.772--UnitOfWork(902782)--Thread(Thread[main,5,main])--end unit of work commit >2) Force a TransactionRequiredException by commenting both the begin()/commit() lines or just the begin() transaction since the code does not get past the update if(JpaHelper.isEclipseLink(entityManagerFactory)) { // getEntityManager().getTransaction().begin(); } aQuery.executeUpdate(); if(JpaHelper.isEclipseLink(entityManagerFactory)) { // getEntityManager().getTransaction().commit(); } >stacktrace excerpt javax.persistence.TransactionRequiredException: Exception Description: No transaction is currently active at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.throwCheckTransactionFailedException(EntityTransactionWrapper.java:113) at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.checkForTransaction(EntityTransactionWrapper.java:50) at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:1664)
Still nor working. Providing requested information: private static void nativeQuery(String query) throws XincoException { getTransaction().begin(); Query q = null; if (isLocked() && locked) { q = getProtectedEntityManager().createNativeQuery(query); } else { q = getEntityManager().createNativeQuery(query); } q.executeUpdate(); if (getTransaction().isActive()) { getTransaction().commit(); } } public static EntityTransaction getTransaction() throws XincoException { EntityTransaction trans = null; if (isLocked() && locked) { trans = getProtectedEntityManager().getTransaction(); } else { trans = getEntityManager().getTransaction(); } return trans; } Here's the relevat stacktraces/output: [EL Finer]: 2010-09-16 11:21:21.883--ServerSession(11649632)--Thread(Thread[main,5,main])--client acquired [EL Finest]: 2010-09-16 11:21:21.883--UnitOfWork(20846071)--Thread(Thread[main,5,main])--Execute query ReadAllQuery(name="XincoSetting.findAll" referenceClass=XincoSetting sql="SELECT id, description, long_value, string_value, bool_value, int_value FROM xinco_setting") [EL Fine]: 2010-09-16 11:21:21.883--ServerSession(11649632)--Connection(28441588)--Thread(Thread[main,5,main])--SELECT id, description, long_value, string_value, bool_value, int_value FROM xinco_setting Sep 16, 2010 11:21:21 AM com.bluecubs.xinco.core.server.XincoDBManager runInitSQL INFO: Running initialization script... [EL Finer]: 2010-09-16 11:21:21.961--UnitOfWork(29530019)--Thread(Thread[Finalizer,8,system])--release unit of work [EL Finer]: 2010-09-16 11:21:21.961--ClientSession(7435043)--Thread(Thread[Finalizer,8,system])--client released [EL Finer]: 2010-09-16 11:21:21.961--UnitOfWork(6620348)--Thread(Thread[Finalizer,8,system])--release unit of work [EL Finer]: 2010-09-16 11:21:21.961--ClientSession(1702354)--Thread(Thread[Finalizer,8,system])--client released [EL Finer]: 2010-09-16 11:21:21.961--UnitOfWork(6169134)--Thread(Thread[Finalizer,8,system])--release unit of work [EL Finer]: 2010-09-16 11:21:21.993--ClientSession(1440568)--Thread(Thread[Finalizer,8,system])--client released Sep 16, 2010 11:21:22 AM com.bluecubs.xinco.core.server.XincoDBManager executeSQL INFO: Executing statement: CREATE TABLE IF NOT EXISTS `xinco`.`xinco_add_attribute_t` ( `record_id` INT(10) UNSIGNED NOT NULL , `xinco_core_data_id` INT(10) UNSIGNED NOT NULL , `attribute_id` INT(10) UNSIGNED NOT NULL , `attrib_int` INT(11) NOT NULL , `attrib_unsignedint` INT(10) UNSIGNED NOT NULL , `attrib_double` DOUBLE NOT NULL , `attrib_varchar` VARCHAR(255) NOT NULL , `attrib_text` TEXT NOT NULL , `attrib_datetime` DATETIME NOT NULL , PRIMARY KEY (`record_id`) ) PACK_KEYS = 0 ROW_FORMAT = DEFAULT; Sep 16, 2010 11:25:18 AM com.bluecubs.xinco.core.server.XincoDBManager runInitSQL SEVERE: null javax.persistence.TransactionRequiredException: Exception Description: No transaction is currently active at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.throwCheckTransactionFailedException(EntityTransactionWrapper.java:113) at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.checkForTransaction(EntityTransactionWrapper.java:50) at org.eclipse.persistence.internal.jpa.EntityManagerImpl.checkForTransaction(EntityManagerImpl.java:1611) at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeUpdate(EJBQueryImpl.java:501) at com.bluecubs.xinco.core.server.XincoDBManager.nativeQuery(XincoDBManager.java:452) at com.bluecubs.xinco.core.server.XincoDBManager.executeSQL(XincoDBManager.java:290) at com.bluecubs.xinco.core.server.XincoDBManager.runInitSQL(XincoDBManager.java:265) at com.bluecubs.xinco.core.server.XincoDBManager.getEntityManagerFactory(XincoDBManager.java:353) at com.bluecubs.xinco.core.server.XincoDBManager.setPU(XincoDBManager.java:256) at com.bluecubs.xinco.core.server.XincoTestCase.setUp(XincoTestCase.java:17) at junit.framework.TestCase.runBare(TestCase.java:128) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052) at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906)
Setting Target and Prioirty. See the following page for details of what these fields mean: http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Similar code works for me: EntityManager em = getEntityManager(); Transaction trans = em.getTransaction(); trans.begin(); em.createNativeQuery(queryString).executeUpdate(); trans.commit(); The problem seems likely to do with the getEntityManager() methods - is there a chance they create a new one each time instead of returning the active one? If so, this will mean the transaction is started on a different EntityManager than the one that the query uses as Micheal suggested. Please factor out the getEntityManager code or upload a test case. Marking this as works for me in the mean time.
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink