Announcement Announcement Module
Collapse
No announcement yet.
javax.persistence.TransactionRequiredException: no transaction is in progress Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • javax.persistence.TransactionRequiredException: no transaction is in progress

    Hi,
    We are getting the following exception
    "javax.persistence.TransactionRequiredExceptio n: no transaction is in progress"
    We are using spring 3.0.5 + hibernate 3.3.2 + JPA 1.0 and websphere application server 7.x

    We have done the following configuration changes

    Websphere admin console
    -------------------------
    1) We have changed the default JPA provider to Hibernate 3.3.2 JPA implementation
    2) We have defined the datasource.

    Persistence.xml file
    ------------------
    <persistence
    xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

    <persistence-unit name="NAPS_JPA" transaction-type="JTA">
    <provider> org.hibernate.ejb.HibernatePersistence </provider>
    <jta-data-source>jdbc/dataSource1.1</jta-data-source>
    <class>com.xx.xx.xx.xx.AccountmasterTbl</class>

    <properties>
    <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFac tory"/>
    <property name="hibernate.transaction.manager_lookup_class"
    value="org.hibernate.transaction.WebSphereExtended JTATransactionLookup"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
    </properties>
    </persistence-unit>
    </persistence>

    Spring configuration file
    -----------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
    http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-2.5.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schem...ng-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-2.5.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schem...g-util-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor" />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionM anager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceXmlLocation" value="META-INF/naps-persistence.xml" />
    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.Hibernat eJpaVendorAdapter">
    <property name="database" value="SQL_SERVER" />
    <property name="showSql" value="true" />
    </bean>
    </property>
    </bean>


    <jee:jndi-lookup id="dataSource "
    jndi-name="jdbc/naps_dataSource1.1"
    cache="true"
    resource-ref="true"
    lookup-on-startup="true"
    proxy-interface="javax.sql.DataSource"/>

    <import resource="padmin_applicationContext.xml" />
    <import resource="pifa_applicationContext.xml" />
    </beans>

    dao class
    ----------
    @Repository
    public class PIFApprovalDAO extends NAPSBaseDAO{

    @PersistenceContext
    EntityManager em;

    @transactional
    public void saveData(FormdetailsTbl formDetailsEntity) {

    EventsTbl eventTbl = em.find(EventsTbl.class,Integer.valueOf(1));
    System.out.println("Event id::"+eventTbl.getEventid());

    RevisionreasonmasterTbl revReason =em.find(RevisionreasonmasterTbl.class,Integer.val ueOf(1));
    System.out.println("Revision id::"+revReason.getRevisionreasonid());

    formDetailsEntity = new FormdetailsTbl();
    formDetailsEntity.setModifiedby("USER");
    formDetailsEntity.setCreatedby("USER");
    formDetailsEntity.setCreateddate(new Timestamp((new Date()).getTime()));
    formDetailsEntity.setModifieddate(new Timestamp((new Date()).getTime()));

    em.persist(formDetailsEntity);
    em.flush();

    System.out.println(".... Entity created");
    }
    }catch (Exception e) {
    e.printStackTrace();


    Tried all options.. but the above error is not going.
    we have even used websphere specific spring transaction manager but the same error.

    Please help me to resolve this issue.

    Thanks
    Ravi

  • #2
    the above error happens on em.persist method

    Comment


    • #3
      Please use [ code][/code ] tags when posting code that way it remains readable...

      You are using the wrong transaction manager, you specify jta in your persistence.xml file but you fail to configure a JTAtransactionManager (<tx:jta-transaction-manager />)..

      So either switch your entitymanager to use local transactions or configure a JTA transactionmanager in stead of a JpaTransactionManager.

      Comment


      • #4
        Sorry,
        i didn't know that I have to use CODE tag, sorry for that.

        Just few queries.
        ---------------
        I have used the following

        Code:
        <bean id="transactionManager"      class="org.springframework.transaction.jta.WebSphereUowTransactionManager"/>
        but without mentioning that <jta tag..will this create an issue?

        Also when i use the above websphereTxNmanager..how can we pass the entityManager reference to it.. or is it not needed.. then how spring knows that it needs to bind entityManager with websphereTxn.

        Thanks
        Ravi

        Comment


        • #5
          Also do we need to specify our datasource as XA datasource for JTA transactions in websphere admin console.

          Comment


          • #6
            If you use mutliple tx resources you need xa datasource if you have a single transactional resource I suggest dropping JTA because it is a lot easier without JTA.

            With JTA you do not need to specify the entitymanager for the transactionamanger, that is why you need a seperate configuration in hibernate to tie the 2 together (for hibernate/jpa). Regarding the xml element, if you have an application for different platforms it is easier to use the jta tag then to specify the transactionmanager yourself. If you only deploy on a single server nothing is wrong with your setup.

            Comment


            • #7
              Hi,
              When we say this

              "With JTA you do not need to specify the entitymanager for the transactionamanger, that is why you need a seperate configuration in hibernate to tie the 2 together (for hibernate/jpa)"

              What are those separate configurations changes which need to be done in hibernate to tie the 2 together.

              I have added the following 2 in persistence.xml file

              Code:
              <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFac tory"/> 
              <property name="hibernate.transaction.manager_lookup_class" 
              value="org.hibernate.transaction.WebSphereExtended JTATransactionLookup"/>

              Are you talking about them or anything else?

              Comment


              • #8
                That are the 2 changes needed to tell hibernate you use jta (and which implementation to use). Transaction management with JTA is quite a different beast as basic transaction management. As a rule of thumb if you don't need multiple transactional resources avoid it, also if you have multiple transactional resources there are also other options instead of JTA. I suggest googling for transactions spring and juergen hoeller he gave a nice presentation about the subject.

                Comment

                Working...
                X