Announcement Announcement Module
Collapse
No announcement yet.
SPRING + HIBERNATE + JPA + JTA + POSTGRES, no commit Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • SPRING + HIBERNATE + JPA + JTA + POSTGRES, no commit

    Hi,

    Im currently trying to use spring with hibernate and jpa, but currently when I try to persist an entity is not been commited

    My persistence.xml is the following

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="2.0" 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_2_0.xsd">
      <persistence-unit name="commons-pu">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.zuca.commons.core.domain.model.UserEntity</class>
        <class>com.zuca.commons.core.domain.model.IdentificationTypeEntity</class>
        <properties/>
      </persistence-unit>
    </persistence>
    and my applicationContext.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
    	   xsi:schemaLocation="
    	   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    <context:component-scan base-package="com.zuca" />
    <context:annotation-config/>
    
    <tx:annotation-driven transaction-manager="transactionManager"/> 
    
    <tx:jta-transaction-manager  /> 
    
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName" value="java:appserver/TransactionManager"/>
    </bean>
    
    	<!-- XA enabled Data source -->
        <jee:jndi-lookup id="dataSource" jndi-name="jdbc/paymentDS"/>
    	    
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
        			p:data-source-ref="dataSource" p:persistence-xml-location="classpath:persistence.xml">
        			<property name="jpaVendorAdapter">
        				<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        					<property name="showSql" value="true" />
                                            <property name="database" value="POSTGRESQL" />
                                            <property name="generateDdl" value="true"/>
        				</bean>
        			</property>
        			<property name="jpaProperties">
        				<value>
        					hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
    			hibernate.transaction.manager_lookup_class=org.hibernate.transaction.SunONETransactionManagerLookup
    					hibernate.current_session_context_class=jta
    					hibernate.transaction.flush_before_completion=true						
    					hibernate.connection.release_mode=auto
                                            hibernate.transaction.jta.platform=org.hibernate.service.jta.platform.internal.SunOneJtaPlatform
                                            hibernate.connection.autocommit=true
        				</value>
        			</property>
                              <property name="loadTimeWeaver">
                                <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
                                </property>
        	</bean>	
            
                <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
            
    
          
    </beans>
    and my classes are the following

    bean
    Code:
    @ManagedBean (name ="registerUserBean")
    @SessionScoped
    @TransactionManagement(TransactionManagementType.CONTAINER) 
    public class RegisterUserBean implements Serializable {  
    
    	private String username;
    	private String email;
    	private String password;
    	private String confirmPassword;
    	private IUserService userService;
    	private String errorMsgRegister;
    
    
    	public RegisterUserBean() {
    		ApplicationContext context = new ClassPathXmlApplicationContext(
    				"applicationContext.xml");
    
    		userService = context.getBean("userService", IUserService.class);
    	}
    
            @TransactionAttribute(TransactionAttributeType.REQUIRED)  
    	public String registerUser(){
    
    
                                        UserDTO userDTO = new UserDTO();
                                        userDTO.setUsername(this.username);
                                        userDTO.setPassword(this.password);
                                        userDTO.setEmail(this.email);
                                        userDTO.setRegisterDate(new Date());
                                        userService.createUser(userDTO);
    
                                    return "login"; 
    	}
    }

    service
    Code:
    public interface IUserService {
        void createUser(UserDTO ue) ;    
    }
    
    @Service("userService")
    @Transactional
    public class UserService implements IUserService,Serializable {
    
    	/** The user repository. */
    	@Autowired
            @Qualifier("userRepository")  
    	private IUserRepository userRepository;
    
            @Override
            @Transactional
    	public void createUser (UserDTO ue) {
    		userRepository.createEntity(UserBuilder.getInstance().build(ue));
    	}

    repository
    Code:
    public interface IUserRepository {
    
        void createEntity(UserEntity entity);
    }
    
    @Repository("userRepository")
    public class UserRepository implements IUserRepository {
    
            
            @PersistenceContext  
            private EntityManager entityManager;  
    	        
    	public void createEntity(UserEntity entity) {
    			entityManager.persist(entity);	
    	}
    }
    I've check the logs and I see the transaction been commited but in the database is not appearing

    INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'userService'

    INFO: testingtest
    INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.interceptor.Trans actionInterceptor#0'

    INFO: org.springframework.transaction.annotation.Annotat ionTransactionAttributeSource - Adding transactional method 'createUser' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''

    INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'transactionManager'

    INFO: org.springframework.transaction.jta.JtaTransaction Manager - Creating new transaction with name [com.zuca.commons.core.application.service.UserServ ice.createUser]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''

    INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Opening JPA EntityManager

    INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Registering transaction synchronization for JPA EntityManager

    INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Closing JPA EntityManager

    INFO: org.springframework.transaction.jta.JtaTransaction Manager - Initiating transaction commit


    My datasource in postgresql is org.postgresql.xa.PGXADataSource


    Tks for your help!

  • #2
    YOu have 2 transaction manager defined one overrides the other either use the namespace of don't.

    Code:
    <tx:jta-transaction-manager  /> 
    
    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName" value="java:appserver/TransactionManager"/>
    </bean>
    Your hibernate configuration has 2 different JTA mechanisms configured.

    Code:
    <property name="jpaProperties">
        				<value>
        					hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
    			hibernate.transaction.manager_lookup_class=org.hibernate.transaction.SunONETransactionManagerLookup
    					hibernate.current_session_context_class=jta
    					hibernate.transaction.flush_before_completion=true						
    					hibernate.connection.release_mode=auto
                                            hibernate.transaction.jta.platform=org.hibernate.service.jta.platform.internal.SunOneJtaPlatform
                                            hibernate.connection.autocommit=true
        				</value>
        			</property>
    You should be fine with only the jta.platform property.

    Your code looks suspicous to me, why are you instantiating a new and fresh application context, use dependency injection instead.

    Code:
    public RegisterUserBean() {
    		ApplicationContext context = new ClassPathXmlApplicationContext(
    				"applicationContext.xml");
    
    		userService = context.getBean("userService", IUserService.class);
    	}
    Also judging by the bean you are using you are mixing JSF2 and Spring. Your JSF bean shouldn't be transactional and, as mentioned earlier, you shouldn't create a new applicationcontext for this bean (JSF integration is explained in the reference guide).

    A final note (not related to your problem) context:annotation-config is already implied by context:component-scan (saves you a line of xml).

    Comment


    • #3
      Hi Marten,

      I've made the adjustments you suggest and the transaction is still not commited

      I remove one of the transaction manager, and the properties on the JPA so the applicationContext.xml now look like this


      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:jee="http://www.springframework.org/schema/jee"
             xmlns:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xmlns:util="http://www.springframework.org/schema/util"
             xmlns:p="http://www.springframework.org/schema/p"
             xmlns:context="http://www.springframework.org/schema/context"
      	   xsi:schemaLocation="
      	   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
             http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
      
      <context:component-scan base-package="com.zuca" />
      
      <tx:annotation-driven transaction-manager="transactionManager"/> 
      
      <tx:jta-transaction-manager  /> 
      
      
      	<!-- XA enabled Data source -->
          <jee:jndi-lookup id="dataSource" jndi-name="jdbc/paymentDS"/>
      	    
          <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
          			p:data-source-ref="dataSource" p:persistence-xml-location="classpath:persistence.xml">
          			<property name="jpaVendorAdapter">
          				<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
          					<property name="showSql" value="true" />
                                              <property name="database" value="POSTGRESQL" />
                                              <property name="generateDdl" value="true"/>
          				</bean>
          			</property>
          			<property name="jpaProperties">
          				<value>
      					hibernate.transaction.flush_before_completion=true						
      					hibernate.connection.release_mode=auto
                                              hibernate.transaction.jta.platform=org.hibernate.service.jta.platform.internal.SunOneJtaPlatform
                                              hibernate.connection.autocommit=true
          				</value>
          			</property>
                                <property name="loadTimeWeaver">
                                  <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
                                  </property>
          	</bean>	
              
            
      </beans>
      also I changed the RegisterUserBean with your suggestions and now it looks like this
      Code:
      @Component
      @Scope("session")
      public class RegisterUserBean implements Serializable {  
      
      	private String username;
      	private String email;
      	private String password;
      	private String confirmPassword;
      
              @Autowired
      	private IUserService userService;
              
      	private String errorMsgRegister;
      
      	public RegisterUserBean() {
      
      	}
      
      	public String registerUser(){
        
                                          UserDTO userDTO = new UserDTO();
                                          userDTO.setUsername(this.username);
                                          userDTO.setPassword(this.password);
                                          userDTO.setEmail(this.email);
                                          userDTO.setRegisterDate(new Date());
                                          userService.createUser(userDTO);
      
                                      return "login"; 
      
      	}
      }
      I still look in the logs and it seem to be commiting something...

      INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Creating instance of bean 'registerUserBean'

      INFO: org.springframework.beans.factory.annotation.Injec tionMetadata - Found injected element on class [com.zuca.mobimark.web.beans.RegisterUserBean]: AutowiredFieldElement for private com.zuca.commons.core.application.service.IUserSer vice com.zuca.mobimark.web.beans.RegisterUserBean.userS ervice

      INFO: org.springframework.beans.factory.annotation.Injec tionMetadata - Processing injected method of bean 'registerUserBean': AutowiredFieldElement for private com.zuca.commons.core.application.service.IUserSer vice com.zuca.mobimark.web.beans.RegisterUserBean.userS ervice

      INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'userService'

      INFO: org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor - Autowiring by type from bean name 'registerUserBean' to bean named 'userService'

      INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTr ansactionAdvisor'

      INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Finished creating instance of bean 'registerUserBean'

      INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'org.springframework.transaction.interceptor.Trans actionInterceptor#0'

      INFO: org.springframework.transaction.annotation.Annotat ionTransactionAttributeSource - Adding transactional method 'createUser' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''

      INFO: org.springframework.beans.factory.support.DefaultL istableBeanFactory - Returning cached instance of singleton bean 'transactionManager'

      INFO: org.springframework.transaction.jta.JtaTransaction Manager - Creating new transaction with name [com.zuca.commons.core.application.service.UserServ ice.createUser]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''

      INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Opening JPA EntityManager

      INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Registering transaction synchronization for JPA EntityManager

      INFO: org.springframework.orm.jpa.EntityManagerFactoryUt ils - Closing JPA EntityManager

      INFO: org.springframework.transaction.jta.JtaTransaction Manager - Initiating transaction commit

      So I dont know what else could be wrong I'm using glassfish 3.1.2 for container and those are the libs

      antlr-2.7.7.jar
      aopalliance-1.0.jar
      c3p0-0.9.1.jar
      cglib-nodep-2.1_3.jar
      commons-core-1.0.0.jar
      commons-lang-2.1.jar
      commons-logging-1.1.1.jar
      core-1.0.0.jar
      dom4j-1.6.1.jar
      ehcache-core-2.4.3.jar
      el-ri-1.0.jar
      hibernate-c3p0-4.2.0.Final.jar
      hibernate-commons-annotations-4.0.1.Final.jar
      hibernate-core-4.2.0.Final.jar
      hibernate-ehcache-4.2.0.Final.jar
      hibernate-entitymanager-4.2.0.Final.jar
      hibernate-jpa-2.0-api-1.0.1.Final.jar
      javassist-3.15.0-GA.jar
      jboss-logging-3.1.0.GA.jar
      jboss-transaction-api_1.1_spec-1.0.0.Final.jar
      jsf-api-2.1.0-b03.jar
      jsf-impl-2.1.0-b03.jar
      jsp-api-2.1.jar
      jstl-1.2.jar
      log4j-1.2.14.jar
      primefaces-3.5.jar
      servlet-api-2.5.jar
      slf4j-api-1.6.1.jar
      spring-aop-3.0.5.RELEASE.jar
      spring-asm-3.0.5.RELEASE.jar
      spring-beans-3.0.5.RELEASE.jar
      spring-context-3.0.5.RELEASE.jar
      spring-context-support-3.0.5.RELEASE.jar
      spring-core-3.0.5.RELEASE.jar
      spring-expression-3.0.5.RELEASE.jar
      spring-jdbc-3.0.5.RELEASE.jar
      spring-orm-3.0.5.RELEASE.jar
      spring-oxm-3.0.5.RELEASE.jar
      spring-tx-3.0.5.RELEASE.jar
      spring-web-3.0.5.RELEASE.jar
      spring-webmvc-3.0.5.RELEASE.jar
      spring-webmvc-portlet-3.0.5.RELEASE.jar

      Comment


      • #4
        You are using Spring 3.0 which hasn't been tested with hibernate 4.2, Spring 3.2 is tested with that hibernate version. So you could try a newer spring version or downgrade hibernate. The newer spring has a property jtaDatasource which triggers the jta transaction type.

        The hibernate.connection properties are pretty much useless as you are injecting a datasource those properties are ignored.

        Looking at your persistence XML it has no setting for the transaction-type.

        Comment


        • #5
          Hi Marten,

          I've update the lib to the 3.2 version, and added the JTA property and datasource to the persistence.xml and is working right now!

          tks for your help

          regards,

          Diego G
          Last edited by kaoru17th; May 30th, 2013, 07:35 AM.

          Comment

          Working...
          X