Announcement Announcement Module
Collapse
No announcement yet.
JTA configuration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JTA configuration

    Can anybody help me with jta transaction manager configuration?

    I have somethink like this:
    Code:
    <bean id="userDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    			<property name="driverClassName">
    				<value>${db.driver}</value>
    			</property>
    			<property name="url">
    				<value>${db.url}</value>
    			</property>
    			<property name="username">
    				<value>${user.name}</value>
    			</property>
    			<property name="password">
    				<value>${user.password}</value>
    			</property>
    						 
    			<property name="maxActive">
    				<value>15</value>
    			</property>			
      	</bean> 
    
    	<bean id="userSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">				
    		<property name="dataSource"><ref local="userDataSource"/></property>
    		<property name="configLocation">
    			<value>/WEB-INF/bivela.user.cfg.xml</value>
    		</property>
    		
    		<property name="jtaTransactionManager">
    			<bean id="jtaTransactionManager.transactionManager" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
    		</property>
    		
    		<property name="hibernateProperties">
    			<props>						
    				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				
    				<prop key="hibernate.max_fetch_depth">3</prop>
    				
    				<prop key="hibernate.current_session_context_class">jta</prop>	
    				
    				<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
    				<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
    				<prop key="hibernate.hbm2dll.auto">create</prop>
    				<prop key="hibernate.connection.release_mode">after_statement</prop>							
    				<prop key="hibernate.connection.pool_size">3</prop>
    				<prop key="hibernate.connection.isolation">2</prop>				
    								
                	<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</prop>
    															
    			</props>
    		</property>
    	</bean>
    
    
    <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">				
    	</bean>		
    
    	<bean id="jtaTransactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    		<property name="transactionManager"><ref local="jtaTransactionManager" /></property>	
    		<property name="transactionAttributeSource">
    			<value> 
    				org.bivela.business.facade.UserFacadeInterface.updateShareholders=PROPAGATION_REQUIRED	
    			</value>
    		</property>
    	</bean>
    
         <bean id="userProxyService" class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="proxyInterfaces">
    			<value>org.bivela.business.facade.UserFacadeInterface</value>
    		</property>
    		<property name="target" ref="userFacade" />
    		<property name="interceptorNames">
    			<list>											
    				<value>jtaTransactionInterceptor</value>	
    				<value>beforeInterceptor</value>			
    			</list>
    		</property>
    	</bean>
    I've tried to use hibernate interceptor but still it doesn't work - doesn't rollback transactions.

    My log file looks like this:

    Code:
    2007-07-03 14:02:13,962 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] Opening Hibernate Session
    2007-07-03 14:02:13,962 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 11834641339
    2007-07-03 14:02:13,962 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] Registering Spring transaction synchronization for new Hibernate Session
    2007-07-03 14:02:13,973 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] Bound value [org.springframework.orm.hibernate3.SessionHolder@192c4c] for key [org.hibernate.impl.SessionFactoryImpl@ab6c19] to thread [http-0.0.0.0-8443-3]
    2007-07-03 14:02:13,974 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] Retrieved value [org.springframework.orm.hibernate3.SessionHolder@192c4c] for key [org.hibernate.impl.SessionFactoryImpl@ab6c19] bound to thread [http-0.0.0.0-8443-3]
    2007-07-03 14:02:13,974 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] Found thread-bound Session for HibernateTemplate
    2007-07-03 14:02:13,983 DEBUG [org.hibernate.jdbc.JDBCContext] successfully registered Synchronization
    2007-07-03 14:02:13,983 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] Retrieved value [org.springframework.orm.hibernate3.SessionHolder@192c4c] for key [org.hibernate.impl.SessionFactoryImpl@ab6c19] bound to thread [http-0.0.0.0-8443-3]
    2007-07-03 14:02:13,984 DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
    2007-07-03 14:02:13,984 DEBUG [org.hibernate.jdbc.ConnectionManager] opening JDBC connection
    
    ...
    
    2007-07-03 14:02:14,288 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] Not closing pre-bound Hibernate Session after HibernateTemplate
    2007-07-03 14:02:14,288 DEBUG [org.springframework.transaction.interceptor.TransactionInterceptor] Completing transaction for [org.bivela.business.facade.UserFacadeInterface.updateShareholders] after exception: org.hibernate.HibernateException: Blad wymuszony
    2007-07-03 14:02:14,288 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] Applying rules to determine whether transaction should rollback on org.hibernate.HibernateException: Blad wymuszony
    2007-07-03 14:02:14,288 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] Winning rollback rule is: null
    2007-07-03 14:02:14,288 DEBUG [org.springframework.transaction.interceptor.RuleBasedTransactionAttribute] No relevant rollback rule found: applying superclass default
    2007-07-03 14:02:14,288 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] Triggering beforeCompletion synchronization
    2007-07-03 14:02:14,295 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] Removed value [org.springframework.orm.hibernate3.SessionHolder@192c4c] for key [org.hibernate.impl.SessionFactoryImpl@ab6c19] from thread [http-0.0.0.0-8443-3]
    2007-07-03 14:02:14,296 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] Closing Hibernate Session
    2007-07-03 14:02:14,296 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] Initiating transaction rollback
    2007-07-03 14:02:14,297 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] Triggering afterCompletion synchronization
    2007-07-03 14:02:14,297 DEBUG [org.springframework.transaction.support.TransactionSynchronizationManager] Clearing transaction synchronization
    2007-07-03 14:02:14,297 DEBUG [org.springframework.web.servlet.DispatcherServlet] Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@78f4
    2007-07-03 14:02:14,298 DEBUG [org.springframework.web.servlet.DispatcherServlet] Could not complete request
    org.hibernate.HibernateException: Blad wymuszony
    	at org.bivela.business.facade.UserFacade.updateShareholders(UserFacade.java:1377)
    Looks like hibernate session is closed before transaction is rolledback.

    What do I have to change in my configuration?

  • #2
    From a first sight: Setting the property hibernate.connection.release_mode to "after_statement" can't work since the connection release enforces a transaction commit after each statement. That should be the same as auto-commit=true if I'm not wrong. I have set it to "auto".

    A second thing I noticed: If you want to use real 2 phase commits (why do you use JTA otherwise?) the BasicDataSource won't work anymore. You need an XADataSource instance.

    Jörg

    Comment


    • #3
      I've changed my data scource configuration to:

      Code:
      <bean id="userDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource" destroy-method="close">
      			<property name="driverName">
      				<value>${db.driver}</value>
      			</property>
      			<property name="url">
      				<value>${db.url}</value>
      			</property>
      			<property name="user">
      				<value>${user.name}</value>
      			</property>
      			<property name="password">
      				<value>${user.password}</value>
      			</property>
      </bean>
      and release_mode to 'auto', then to 'after_transaction'

      Still doesn't work

      Comment


      • #4
        You still inject this userDataSource directly into the SessionFactory? That's not possible. The setup of JTA transaction management is a bit more complex. What actual TransactionManager do you use (not the Spring one)? I for example use Jencks with Geronimo.

        Jörg

        Comment


        • #5
          I'm useing JBoss so I think that I don't have to set it up in my configuration.

          I checked in log file and everything is ok:

          Code:
          2007-07-03 18:03:29,679 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] Retrieving JTA TransactionManager from JNDI location [java:/TransactionManager]
          2007-07-03 18:03:29,679 DEBUG [org.springframework.jndi.JndiTemplate] Looking up JNDI object with name [java:/TransactionManager]
          2007-07-03 18:03:29,679 DEBUG [org.springframework.transaction.jta.JtaTransactionManager] JTA TransactionManager found at fallback JNDI location [java:/TransactionManager]
          2007-07-03 18:03:29,679 INFO  [org.springframework.transaction.jta.JtaTransactionManager] Using JTA UserTransaction: org.jboss.tm.usertx.client.ServerVMClientUserTransaction@13cb2c6
          2007-07-03 18:03:29,680 INFO  [org.springframework.transaction.jta.JtaTransactionManager] Using JTA TransactionManager: org.jboss.tm.TxManager@1fd245f
          and my database is in MySQL (I use InnoDB tables)

          Comment


          • #6
            Sounds reasonable. But then you must retrieve your DataSource from JNDI as well if I'm not mistaken.

            Jörg

            Comment


            • #7
              I'm not sure I understand you. Could you be more specific?

              I've something like this:
              Code:
              2007-07-03 18:03:37,159 INFO  [org.hibernate.connection.ConnectionProviderFactory] Initializing connection provider: org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider
              2007-07-03 18:03:37,251 DEBUG [org.enhydra.jdbc.xapool] StandardDataSource:getConnection a new driver instance is created
              2007-07-03 18:03:38,196 DEBUG [org.enhydra.jdbc.xapool] StandardDataSource:getConnection Connection from DriverManager is returned
              2007-07-03 18:03:38,198 INFO  [org.hibernate.cfg.SettingsFactory] RDBMS: MySQL, version: 5.0.24a-Debian_9ubuntu0.1-log
              2007-07-03 18:03:38,198 INFO  [org.hibernate.cfg.SettingsFactory] JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.4 ( $Date: 2006-10-19 17:47:48 +0200 (Thu, 19 Oct 2006) $, $Revision: 5908 $ )
              2007-07-03 18:03:38,362 INFO  [org.hibernate.dialect.Dialect] Using dialect: org.hibernate.dialect.MySQLInnoDBDialect
              2007-07-03 18:03:38,381 INFO  [org.hibernate.transaction.TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.JTATransactionFactory
              2007-07-03 18:03:38,446 INFO  [org.hibernate.util.NamingHelper] JNDI InitialContext properties:{}
              2007-07-03 18:03:38,457 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.springframework.orm.hibernate3.LocalTransactionManagerLookup
              2007-07-03 18:03:38,458 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] instantiated TransactionManagerLookup
              2007-07-03 18:03:38,458 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.springframework.orm.hibernate3.LocalTransactionManagerLookup
              2007-07-03 18:03:38,459 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] instantiated TransactionManagerLookup
              2007-07-03 18:03:38,459 INFO  [org.hibernate.cfg.SettingsFactory] Automatic flush during beforeCompletion(): enabled
              2007-07-03 18:03:38,459 INFO  [org.hibernate.cfg.SettingsFactory] Automatic session close at end of transaction: enabled
              2007-07-03 18:03:38,460 INFO  [org.hibernate.cfg.SettingsFactory] JDBC batch size: 15
              2007-07-03 18:03:38,460 INFO  [org.hibernate.cfg.SettingsFactory] JDBC batch updates for versioned data: disabled
              2007-07-03 18:03:38,462 INFO  [org.hibernate.cfg.SettingsFactory] Scrollable result sets: enabled
              2007-07-03 18:03:38,463 DEBUG [org.hibernate.cfg.SettingsFactory] Wrap result sets: disabled
              2007-07-03 18:03:38,463 INFO  [org.hibernate.cfg.SettingsFactory] JDBC3 getGeneratedKeys(): enabled
              2007-07-03 18:03:38,463 INFO  [org.hibernate.cfg.SettingsFactory] Connection release mode: after_transaction
              2007-07-03 18:03:38,466 INFO  [org.hibernate.cfg.SettingsFactory] Maximum outer join fetch depth: 3
              2007-07-03 18:03:38,466 INFO  [org.hibernate.cfg.SettingsFactory] Default batch fetch size: 1
              2007-07-03 18:03:38,519 INFO  [org.hibernate.cfg.SettingsFactory] Generate SQL with comments: disabled
              2007-07-03 18:03:38,520 INFO  [org.hibernate.cfg.SettingsFactory] Order SQL updates by primary key: disabled
              2007-07-03 18:03:38,520 INFO  [org.hibernate.cfg.SettingsFactory] Order SQL inserts for batching: disabled
              2007-07-03 18:03:38,520 INFO  [org.hibernate.cfg.SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
              2007-07-03 18:03:38,535 INFO  [org.hibernate.hql.ast.ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
              2007-07-03 18:03:38,536 INFO  [org.hibernate.cfg.SettingsFactory] Query language substitutions: {}
              2007-07-03 18:03:38,536 INFO  [org.hibernate.cfg.SettingsFactory] JPA-QL strict compliance: disabled
              2007-07-03 18:03:38,537 INFO  [org.hibernate.cfg.SettingsFactory] Second-level cache: enabled
              2007-07-03 18:03:38,538 INFO  [org.hibernate.cfg.SettingsFactory] Query cache: disabled
              2007-07-03 18:03:38,538 INFO  [org.hibernate.cfg.SettingsFactory] Cache provider: org.hibernate.cache.NoCacheProvider
              2007-07-03 18:03:38,590 INFO  [org.hibernate.cfg.SettingsFactory] Optimize cache for minimal puts: disabled
              2007-07-03 18:03:38,590 INFO  [org.hibernate.cfg.SettingsFactory] Structured second-level cache entries: disabled
              2007-07-03 18:03:38,611 INFO  [org.hibernate.cfg.SettingsFactory] Echoing all SQL to stdout
              2007-07-03 18:03:38,661 INFO  [org.hibernate.cfg.SettingsFactory] Statistics: disabled
              2007-07-03 18:03:38,662 INFO  [org.hibernate.cfg.SettingsFactory] Deleted entity synthetic identifier rollback: disabled
              2007-07-03 18:03:38,662 INFO  [org.hibernate.cfg.SettingsFactory] Default entity-mode: pojo
              2007-07-03 18:03:38,662 INFO  [org.hibernate.cfg.SettingsFactory] Named query checking : enabled
              2007-07-03 18:03:38,784 INFO  [org.hibernate.impl.SessionFactoryImpl] building session factory
              2007-07-03 18:03:38,787 DEBUG [org.hibernate.impl.SessionFactoryImpl] Session factory constructed with filter configurations : {}
              Is it ok?

              Comment


              • #8
                The connection management of the data source must be under control of the TransactionManager. If you set the data source up the way you are doing it the TransactionManager is not aware of it and can't control its transactions. In a so called managed environment the data source is normally retrieved via JNDI from the container as well. If you have a look at the link for Jencks I provided you see for example that there is a LocalConnectionFactoryBean name connectionFactory. That is actually the data source. All the wiring stuff with ManagedConnectionFactory, TransactionManager, etc. that's what the container is supposed to do transparently in a managed environment. Since you set up your data source "plainly" in Spring all that stuff is missing.

                Jörg

                Comment


                • #9
                  But in my opionion I don't need Jencks tools, because I'm using JBoss which is J2EE container and there is written that Jencks is support for Spring outside a J2EE server.

                  Besides I think that I have database managed by transaction manager. This setting is in userSessionFactory where I set my data source and point to transaction manager from JTA Transaction Manager:

                  Code:
                  <bean id="userSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">				
                  		<property name="dataSource"><ref local="userDataSource"/></property>
                  
                  		<property name="jtaTransactionManager">
                  			<bean id="jtaTransactionManager.transactionManager" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
                  		</property>
                  ....
                  I think it's sufficient to link these two things.

                  Comment


                  • #10
                    Originally posted by martique View Post
                    But in my opionion I don't need Jencks tools
                    Indeed you don't need them. You only missed my point ...

                    Originally posted by martique View Post
                    Besides I think that I have database managed by transaction manager.
                    No, you don't. That's exactly the point I wanted to make. You have an unmanaged data source since you set it up yourself in the Spring application context. If I see it correctly the StandardXADataSource is part of XAPool and (AFAIR) it implements both XADataSource and DataSource which probably fools you and everything seems to be set up correctly - but doesn't work.

                    See what I have in my application context with Jencks:

                    Code:
                    <bean id="xaDataSource" class="com.ibm.db2.jcc.DB2XADataSource">
                      <!-- properties -->
                    </bean>
                    
                    <bean id="xaDataSourceManagedConnectionFactory" class="com.company.XADataSourceMCF">
                      <constructor-arg ref="xaDataSource"/>
                    </bean>
                    
                    <bean id="dataSource" class="org.springframework.jca.support.LocalConnectionFactoryBean">
                      <property name="managedConnectionFactory" ref="xaDataSourceManagedConnectionFactory"/>
                      <property name="connectionManager" ref="connectionManager"/>
                    </bean>
                    To compare it with your setup: xaDataSource is your StandardXADataSource. But since it also implements interface DataSource (AFAIR) it works also as dataSource and you are able to inject it into LocalSessionFactoryBean but you MUST NOT do this. You miss the ManagedConnectionFactory part. That one is asked for the actual DataSource via createConnectionFactory(ConnectionManager).

                    If you had a managed data source you would not need to care about that stuff. The container gives you a DataSource instance and handles the XADataSource stuff internally.

                    Originally posted by martique View Post
                    This setting is in userSessionFactory where I set my data source and point to transaction manager from JTA Transaction Manager
                    That's not sufficient.

                    If you want to learn more about the details I can recommend XA exposed by Mike Spille.

                    Jörg

                    Comment

                    Working...
                    X