Search code examples
jpaopenjpa

Can only perform operation while a transaction is active


I am using OpenJPA in cxf(jax-rs) + Struts 2. No external transaction manager. Why am I getting "Can only perform operation while a transaction is active."?

My persistence.xml is like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="myPU"
        transaction-type="RESOURCE_LOCAL">

        <class>com.bb.bba.entity.Advertisement</class>
        <class>com.bb.bba.entity.Poi</class>

        <properties>
            <property name="openjpa.ConnectionDriverName" value="org.apache.derby.jdbc.ClientDriver" />
            <property name="openjpa.ConnectionURL" value="jdbc:derby://localhost:1527/bba;create=true" />
            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" />
            <property name="openjpa.DynamicEnhancementAgent" value="false" />
            <property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
            <property name="openjpa.ConnectionUserName" value="APP" />
            <property name="openjpa.ConnectionPassword" value="asdasd" />
            <property name="openjpa.Log" value="SQL=TRACE"/>
        </properties>
    </persistence-unit>
</persistence>

I am getting the following error.

35692  2012-06-02 03:50:57,847 DEBUG [http-33791-1] resolver.DefaultTraversableResolver.initJpa (DefaultTraversableResolver.java:77) - Found javax.persistence.PersistenceUtil on classpath.
35729  2012-06-02 03:50:57,884 DEBUG [http-33791-1] resolver.DefaultTraversableResolver.initJpa (DefaultTraversableResolver.java:91) - Instantiated an instance of org.apache.bval.jsr303.resolver.JPATraversableResolver.
35755  2012-06-02 03:50:57,910 TRACE [http-33791-1] jsr303.DefaultMessageInterpolator.loadBundle (DefaultMessageInterpolator.java:189) - ValidationMessages not found by thread local classloader
35755  2012-06-02 03:50:57,910 TRACE [http-33791-1] jsr303.DefaultMessageInterpolator.loadBundle (DefaultMessageInterpolator.java:189) - ValidationMessages not found by validator classloader
35756  2012-06-02 03:50:57,911 DEBUG [http-33791-1] jsr303.DefaultMessageInterpolator.getFileBasedResourceBundle (DefaultMessageInterpolator.java:176) - ValidationMessages not found. Delegating to org.apache.bval.jsr303.ValidationMessages
35778  2012-06-02 03:50:57,933 DEBUG [http-33791-1] xml.ValidationParser.parseXmlConfig (ValidationParser.java:88) - No META-INF/validation.xml found. Using annotation based configuration only.
646  myPU  INFO   [http-33791-1] openjpa.Runtime - OpenJPA dynamically loaded a validation provider.
922  myPU  INFO   [http-33791-1] openjpa.Runtime - Starting OpenJPA 2.1.1
1703  myPU  INFO   [http-33791-1] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.DerbyDictionary".
11326  myPU  WARN   [http-33791-1] openjpa.Enhance - Creating subclass for "[class com.bb.bba.entity.Poi, class com.bb.bba.entity.Advertisement]". This means that your application will be less efficient and will consume more memory than it would if you ran the OpenJPA enhancer. Additionally, lazy loading will not be available for one-to-one and many-to-one persistent attributes in types using field access; they will be loaded eagerly instead.

Here I am

...... nonfatal user error> org.apache.openjpa.persistence.TransactionRequiredException: Can only perform operation while a transaction is active. at org.apache.openjpa.kernel.BrokerImpl.assertActiveTransaction(BrokerImpl.java:4658) at org.apache.openjpa.kernel.DelegatingBroker.assertActiveTransaction(DelegatingBroker.java:1386) at org.apache.openjpa.persistence.EntityManagerImpl.flush(EntityManagerImpl.java:661) at com.bb.bba.dao.MyDao.createPoi(MyDao.java:21) at com.bb.bba.services.Service.createPoi(POIService.java:36)


Solution

  • The problem is solved. I was using resource_local which means I am responsible for the transaction. Adding em.getTransaction.begin solved the issue.