Results 1 to 8 of 8

Thread: Using JTA with Spring +hibernate JPA+Glassfish 3.1 Transaction not committing

  1. #1
    Join Date
    Dec 2005
    Posts
    23

    Default Using JTA with Spring +hibernate JPA+Glassfish 3.1 Transaction not committing

    I have been struggling with this issues and i have searched the entire internet but to no avail. I am hoping you guy can help me I using glassfish 3.1 with hibernate JPA and spring 3. I have configured a datasource and pool in Glassfish to target MYSQL please find my configurations

    the Web.xml , i configured the persistence units.

    <persistence-unit-ref>
    <description>Persistence Unit for PlyPlus</description>
    <persistence-unit-ref-name>
    persistence/zeneJPA
    </persistence-unit-ref-name>
    <persistence-unit-name>
    ZeneJPA
    </persistence-unit-name>
    </persistence-unit-ref>
    The application-context

    <context:component-scan base-package="com.binarydna.store" />
    <bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor">
    <property name="persistenceUnits">
    <map>
    <entry key="ZeneJPA" value="persistence/zeneJPA">
    </entry>
    </map>
    </property>
    </bean>
    <!--
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityMana gerFactoryBean">
    <property name="persistenceUnitName" value="ZeneJPA"/>
    </bean>
    -->
    <!-- We want to use Spring's declarative @Transaction management -->
    <tx:annotation-driven />
    <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/zeneJPA" />
    <tx:jta-transaction-manager />
    Persistence.xml

    <persistence-unit name="ZeneJPA" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence </provider>
    <jta-data-source>jdbc/zeneDS</jta-data-source>
    <properties>
    <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransaction ManagerLookup"/>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.generate_statistics" value="true"/>
    <property name="hibernate.archive.autodetection" value="class,hbm"/>
    <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFac tory"/>
    the method/class that saves the object

    @Repository("AdminDAO")
    @Transactional
    public class AdminDAOImpl implements AdminDAO {

    @PersistenceContext
    private EntityManager em;


    public void setEntityManager(EntityManager em) {
    this.em = em;
    }

    @Override
    public boolean saveCustomer(Customer customer) {
    boolean saved = false;
    try {
    if (customer.getId() == null) {
    this.em.persist(customer);
    }
    else {
    this.em.merge(customer);
    }
    saved = true;
    }
    catch (Exception p) {
    saved = false;
    return saved;
    }
    return saved;
    }
    }
    Please help me out here what am i doing wrong?? This configuration generates the tables but just does not commit the transaction. When i step through using the degugger it actually calls the save but does not commit the transaction. Please what is the appropraite configuration to get spring transaction working with Glassfish? Thanks I appreciate all the help.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Please use [ code][/code ] tags when posting code, that way it remains readable... For starters when use transaction management from spring NEVER catch the exception this will break proper transaction management (the transaction interceptor doesn't see an exception and thus doesn't rollback but tries to commit).

    Next the actual transactional layer should be your service layer and not your daos'

    Also please post the code that you use to test this. In your debugging check if there is a TransactionInterceptor somewhere in your call stack.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Dec 2005
    Posts
    23

    Default

    hello sir,
    Thanks for the reply.
    I followed your recommendation and removed the try and catch as well as transferring the @transaction to the service layer.
    when i debugging again the data was not committed to the database.
    So I added flush() after persist() and I got this exception.
    TransactionRequiredException.
    No externally managed transaction is currently active for this thread.

    what do i do please?
    Isn't there like a standard config for spring,JPA+glassfish using JTA?
    I appreciate any help.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    As I already stated check your call stack if the transaction interceptor is there if it isn't you have a tx setup problem.

    Also make sure you don't have a duplicate component-scan (one in your servlet.xml and one in your contextloaderlistener.xml that way you have duplicate instances).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Dec 2005
    Posts
    23

    Default

    Thanks a lot . That was it . There were duplication "component-scan" both in the -servlet.xml and *applicationContext.xml.
    Everything is working now.
    Thanks again.

  6. #6
    Join Date
    Dec 2005
    Posts
    23

    Default

    thanks a lot for the help Mark.
    I ran into another issue while trying to use the applicationContext-jpa in my webflow application.
    i imported it into the web-application.xml config.
    Code:
    <import resource="webmvc-config.xml" />
    	<import resource="webflow-config.xml" />
    	 <import  resource="classpath:applicationContext-jpa.xml" />

    This is my flow.
    Code:
    <persistence-context />
    <var class="com.binarydna.store.domain.Customer" name="userreg" />
    	<view-state id="initpage" >
    	<on-render>
    			<render fragments="body" />
    		</on-render>
    	<transition on="next" to="personalInfo" />
    	</view-state>
    	<view-state id="personalInfo"  model="userreg" >
               <on-render>
    			<render fragments="body" />
    		</on-render>
      </view-state>
    
    	
    </flow>
    When I deloy everything is fine but when i try to access my flow
    I am getting this error. please help.
    Other spring mvc parts of my application works fine

  7. #7
    Join Date
    Dec 2005
    Posts
    23

    Default

    thanks a lot for the help Mark.
    I ran into another issue while trying to use the applicationContext-jpa in my webflow application.
    i imported it into the web-application.xml config.
    Code:
    <import resource="webmvc-config.xml" />
    	<import resource="webflow-config.xml" />
    	 <import  resource="classpath:applicationContext-jpa.xml" />

    This is my flow.
    Code:
    <persistence-context />
    <var class="com.binarydna.store.domain.Customer" name="userreg" />
    	<view-state id="initpage" >
    	<on-render>
    			<render fragments="body" />
    		</on-render>
    	<transition on="next" to="personalInfo" />
    	</view-state>
    	<view-state id="personalInfo"  model="userreg" >
               <on-render>
    			<render fragments="body" />
    		</on-render>
      </view-state>
    
    	
    </flow>
    I also config the jpaflow listener in my *flow-config
    Code:
     <bean id="jpaFlowExecutionListener"
    class="org.springframework.webflow.persistence.JpaFlowExecutionListener">
    <constructor-arg ref="entityManagerFactory" />
    <constructor-arg ref="transactionManager" />
    </bean>
    <webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry"> 
    		<webflow:flow-execution-listeners>
    		<!--	<webflow:listener ref="securityFlowExecutionListener" />-->
    		<webflow:listener ref="jpaFlowExecutionListener" />
    		</webflow:flow-execution-listeners>
    		
    	</webflow:flow-executor>
    When I deloy everything is fine but when i try to access my flow
    I am getting this error.
    Code:
    [#|2012-03-12T21:34:41.664+0100|WARNING|glassfish3.1|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=40;_ThreadName=http-thread-pool-8080(4);|StandardWrapperValve[plyplus]: PWC1406: Servlet.service() for servlet plyplus threw exception
    java.lang.IllegalStateException: ResourceAdapter not initialized
    	at com.sun.gjc.spi.ResourceAdapter.getInstance(ResourceAdapter.java:88)
    	at com.sun.gjc.spi.ManagedConnectionFactory.readExternal(ManagedConnectionFactory.java:1458)
    	at java.io.ObjectInputStream.readExternalData(ObjectInputStream.java:1810)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1769)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
    	at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    	at java.util.HashMap.readObject(HashMap.java:1043)
    	at sun.reflect.GeneratedMethodAccessor49.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
    	at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
    	at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
    	at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1888)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
    	at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:498)
    	at org.hibernate.ejb.AbstractEntityManagerImpl.readObject(AbstractEntityManagerImpl.java:1237)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
    	at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    	at java.util.HashMap.readObject(HashMap.java:1043)
    	at sun.reflect.GeneratedMethodAccessor49.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
    	at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1964)
    	at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:498)
    	at org.springframework.webflow.core.collection.LocalAttributeMap.readObject(LocalAttributeMap.java:331)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991)
    	at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771)
    	
    	:1771)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    	at org.springframework.webflow.engine.impl.FlowExecutionImpl.readExternal(FlowExecutionImpl.java:307)
    	at java.io.ObjectInputStream.readExternalData(ObjectInputStream.java:1810)
    	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1769)
    	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347)
    	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    	at org.springframework.webflow.execution.repository.snapshot.SerializedFlowExecutionSnapshot.deserialize(SerializedFlowExecutionSnapshot.java:194)
    	at org.springframework.webflow.execution.repository.snapshot.SerializedFlowExecutionSnapshot.unmarshal(SerializedFlowExecutionSnapshot.java:99)
    	at org.springframework.webflow.execution.repository.snapshot.SerializedFlowExecutionSnapshotFactory.restoreExecution(SerializedFlowExecutionSnapshotFactory.java:80)
    	at org.springframework.webflow.execution.repository.snapshot.AbstractSnapshottingFlowExecutionRepository.restoreFlowExecution(AbstractSnapshottingFlowExecutionRepository.java:89)
    	at org.springframework.webflow.execution.repository.impl.DefaultFlowExecutionRepository.getFlowExecution(DefaultFlowExecutionRepository.java:115)
    	at org.springframework.webflow.executor.FlowExecutorImpl.resumeExecution(FlowExecutorImpl.java:168)
    	at org.springframework.webflow.mvc.servlet.FlowHandlerAdapter.handle(FlowHandlerAdapter.java:183)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:874)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:779)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:735)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
    	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
    	at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    please help.
    Other spring mvc parts of my application works fine

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Why are you loading the jpa stuff again? It is already loaded by the contextloaderlistener and as such already available to your webflow configuration.

    Judging from the stacktrace the problem is the fact that the JNDI based entitymanager(factory) is being serialized. But I don't have a solution ready for that.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •