Announcement Announcement Module
Collapse
No announcement yet.
Spring + Hibernate + JOTM + MySQL = threads are not correctly closed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + Hibernate + JOTM + MySQL = threads are not correctly closed

    Hi community,

    I'm new on developing and I have being configuring my application based on your help since few months ago.

    Everything seems to be working fine with JPA, until my application start asking for userTransaction support.

    I found that I should add JTA support in my build path and everything starts working again until I try to publish my application using Tomcat 6.0 over LiveCycleDataService from Adobe.

    It seems that JTA jars were generating some incompatilibility problems with JOTM jars provided in Tomcat, so I started looking after comments and suggestions in the forum and I was able to make it work... but - and there is always a but in this project - I'm facing a basic problem... my main end but there are still some threads that keep runnig until I terminate the excecution...

    I debug my app and found nothing... no errors in log4J, no alerts... but I'm sure that something is wrong in my Spring configuration files...

    If anyone can help me to find out what is going on here it would really help me to start developing my system...

    Next I'm leaving the spring-context.xml I'm using, and the persistence.xml file:

    spring-context.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:aop="http://www.springframework.org/schema/aop"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                               http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                               http://www.springframework.org/schema/tx
                               http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                               http://www.springframework.org/schema/aop
                               http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
     
      <!-- enables interpretation of the @Required annotation to ensure that dependency injection actually occures -->
      <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>  
     
      <!-- enables interpretation of the @PersistenceUnit/@PersistenceContext annotations providing convenient
           access to EntityManagerFactory/EntityManager -->
      <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
     
      <!-- first XA data source -->
      <bean id="bdbInnerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource">
        <property name="driverName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/medicalcare"/>
      </bean>
     
      <!-- first XA data source pool -->
      <bean id="bdbDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
        <property name="transactionManager" ref="jotm"/>
        <property name="dataSource" ref="bdbInnerDataSource"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
        <property name="maxSize" value="4"/>
      </bean>
     
      <!-- second XA data source 
      <bean id="bpeInnerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource">
        <property name="transactionManager" ref="jotm"/>
        <property name="driverName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/medicalcare"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
      </bean>
     -->
      <!-- second XA data source pool 
      <bean id="bpeDataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
        <property name="transactionManager" ref="jotm"/>
        <property name="dataSource" ref="bpeInnerDataSource"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
        <property name="maxSize" value="4"/>
      </bean>
     -->
      <!-- required cos the standalone JOTM transaction manager is not autodetected by the
           JtaTransactionManager cos it requires a static accessor method -->
      <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean"/>
     
      <!-- supplying the JotmFactoryBean merely to the userTransaction property cos JtaTransactionManager
           autodetects that the object returned by the JotmFactoryBean implements both the
           UserTransaction and the TransactionManager interface-->
      <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="userTransaction" ref="jotm"/>
        <property name="allowCustomIsolationLevels" value="true"/>
      </bean>   
     
      <!-- settings previously specified in the persistence.xml JPA configuration file are now defined with the
           LocalContainerEntityManagerFactoryBean configuration -->  
      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">    
        <!-- reference to the XA datasource -->
        <property name="dataSource" ref="bdbDataSource"/>
        <!-- specify Hibernate as the the JPA provider -->
        <property name="jpaVendorAdapter">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
            <property name="generateDdl" value="false"/>
            <property name="database" value="MYSQL"/>
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
          </bean>
        </property>
        <!-- configure Hibernate to participate in JTA transactions using the JOTM transaction manager and
             specify further Hibernate specific configuration properties -->
        <property name="jpaPropertyMap">
          <map>
         	 <entry key="hibernate.connection.release_mode" 
         	 	value="on_close"/>
            <entry key="hibernate.transaction.manager_lookup_class"
                   value="org.hibernate.transaction.JOTMTransactionManagerLookup"/>
            <entry key="hibernate.transaction.flush_before_completion"
                   value="true"/>
            <entry key="hibernate.transaction.auto_close_session"
                   value="false"/>
            <entry key="hibernate.current_session_context_class"
                   value="jta"/>
            <entry key="hibernate.connection.release_mode"
                   value="auto"/>                            
          </map>
        </property>
        <!-- specify that the Hibernate JPA dialect should be used, probably not necessary since 
             HibernateJpaVendorAdapter will most likely set this property -->
        <property name="jpaDialect">
          <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
        </property>
        <!-- custom implementation to enrich the PersistenceUnitInfo read from the persistence.xml
             JPA configuration file with the JTA datasource. specifying the JTA datasource directly in
             the Spring configuration file has the advantage that we can use a direct reference to the
             datasource instead of using a JNDI name as requied by the jta-data-source setting in the
             persistence.xml file -->
        <property name="persistenceUnitPostProcessors">
          <bean class="ar.medicalcare.persistence.JtaPersistenceUnitPostProcessor">
            <property name="jtaDataSource" ref="bdbDataSource"/>		
          </bean>
        </property>
      </bean>
     	<!-- Repositories -->
    	<bean id="patientRepository"
    		class="ar.medicalcare.persistence.PatientRepositoryJPA" />
    	<bean id="employeeRepository"
    		class="ar.medicalcare.persistence.EmployeeRepositoryJPA" />
      <!-- enables interpretation of the @Transactional annotation for declerative transaction managment
           using the specified JtaTransactionManager -->
      <tx:annotation-driven transaction-manager="jtaTransactionManager" proxy-target-class="false"/>
     
      <!-- enables interpretation of the @Configurable annotation for domain object dependency injection -->
      <aop:spring-configured/>
    </beans>
    persistence.xml:

    Code:
    <?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"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                                     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
                 version="1.0">
      <!-- transaction type set to JTA, transaction suspension and XA supported -->
      <persistence-unit name="SpringConfiguredPersistenceUnit" transaction-type="JTA"/>
    </persistence>
    Many thanks for any kind of suggestions/comment you can give me.
Working...
X