Results 1 to 3 of 3

Thread: Using EJB Transaction in a non-JTA environment. Help!

  1. #1
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    54

    Default Using EJB Transaction in a non-JTA environment. Help!

    Hi,

    I am building a persistence layer using Spring 1.2.3 and Hibernate 3.0.5 to be incorporated in a Websphere 4.0.6 environment (JDK1.3.1), with a Sybase DB. The Sybase driver (or Sybase version) cannot be JTA compliant. I read that some DAO/Spring pattern cannot be use. I hope I am not using it...

    I would like my DAO to participate in the current CMT transaction. It mean that Hibernate/spring should never create its own transaction, or commit it.

    Also, I would like that the Hibernate Session be associate to the CMT transaction (in a Session per transaction pattern). I could then use the Hibernate Pojo (browse inside) inside the EJB methode without worrying if the connection is close or not.

    I think I have manage to make things works, but I still have problem with the transaction and session issues configuration/programmatic. Could anyone help with this?


    This is my DAO implementation:
    Code:
    /*
    * Bookeeping is called with a composit key (request + SeqId)
    /*
    public class BookkeepingDaoImpl extends HibernateDaoSupport implements BookkeepingDao {
    
    	public Bookkeeping findBookkeepingById(int fkRequest_UID, int seqNo){
    		BookkeepingId bid = new BookkeepingId(request, seqNo);
    		Bookkeeping bk = (Bookkeeping)getHibernateTemplate().load(Bookkeeping.class, bid);
    		System.out.println("testing Bookkeeping getSettlementDate: " + bk.getSettlementDate());
    		return bk;
    	}
    }

    and this is my mapping:


    Code:
    <!-- THIS Connection JNDI- - mlevel test only -->
    	<!-- JNDI Environment Template -->
        <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
            <property name="environment">
                <props>
                    <prop key="java.naming.factory.initial">com.ibm.websphere.naming.WsnInitialContextFactory</prop>
                    <prop key="java.naming.provider.url">iiop&#58;//10.160.97.104&#58;900</prop>
                </props>
            </property>
        </bean>
    
        
    	<bean id="data-source" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiTemplate">
                <ref bean="jndiTemplate"/>
            </property>
            <property name="jndiName">
    			<value>jdbc/sybasedev</value>
    		</property>
    	</bean>
    
    	<!--bookkeeping DAO -->
    	<bean id="bookkeepingDao" class="com.eg.dao.impl.BookkeepingDaoImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    
    	<bean id="sessionFactory"
    		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="mappingResources">
    			<list>
    		
    		
    				<value>com/eg/model/Bookkeeping.hbm.xml</value>
    				
    				
    				</list>
    		</property>
    		
    	
    		 
    		
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.SybaseDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				
    				<prop key="hibernate.generate_statistics">true</prop>
    				
    			</props>
    		</property>
    		<property name="dataSource"><ref bean="data-source" /></property>
    	</bean>
    An this is the EJB calling the DAO implementation of Bookeeping:

    Code:
    /**
     * Bean implementation class for Enterprise Bean&#58; TestTransactionBean
     */
    public class TestTransactionBeanBean extends AbstractStatelessSessionBean&#123;
    	private javax.ejb.SessionContext mySessionCtx;
    
    	private BookkeepingDao bookkeepingDao;
    	
    	/* &#40;non-Javadoc&#41;
    	 * @see org.springframework.ejb.support.AbstractStatelessSessionBean#onEjbCreate&#40;&#41;
    	 */
    	protected void onEjbCreate&#40;&#41; throws CreateException &#123;
    		bookkeepingDao = &#40;BookkeepingDao&#41;getBeanFactory&#40;&#41;.getBean&#40;"bookkeepingDao"&#41;;
    	&#125;
    
    	/**
    	 * setSessionContext
    	 */
    	public void setSessionContext&#40;javax.ejb.SessionContext ctx&#41; &#123;
    		super.setSessionContext&#40;ctx&#41;;
    		setBeanFactoryLocator&#40;ContextSingletonBeanFactoryLocator.getInstance&#40;"classpath*&#58;test-main-context.xml"&#41;&#41;;
    		setBeanFactoryLocatorKey&#40;EjbConstants.BEAN_FACTORY_JNDI&#41;;
    	&#125;
    
    	public void testSpringDaoAccess&#40;&#41; &#123;
    		Bookkeeping bk = bookkeepingDao.findBookkeepingById&#40;6175, 1&#41;;
    		System.out.println&#40;"bk.getAmount&#40;&#41;" + bk.getAmount&#40;&#41;&#41;;
    	&#125;
    	
    
    &#125;

    When I run the EJB and try to call the testSpringDaoAccess method, I get a Exception telling me the session has been closed. I dont quite well understand the life-cycle of the Session and Transaction. I put the log to DEBUG for Hibernate and this is what I got:

    Code:
    &#91;22/08/05 12&#58;59&#58;44&#58;355 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;44,355 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;SessionFactoryObjectFactory.java&#58;39&#41; - initializing class SessionFactoryObjectFactory
    
    &#91;22/08/05 12&#58;59&#58;44&#58;355 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;44,355 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;SessionFactoryObjectFactory.java&#58;76&#41; - registered&#58; 8a20e1e805df21880105df2195430000 &#40;unnamed&#41;
    
    &#91;22/08/05 12&#58;59&#58;44&#58;355 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;44,355  INFO &#91;Servlet.Engine.Transports&#58;10&#93; &#40;SessionFactoryObjectFactory.java&#58;82&#41; - Not binding factory to JNDI, no JNDI name configured
    
    &#91;22/08/05 12&#58;59&#58;44&#58;355 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;44,355 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;SessionFactoryImpl.java&#58;262&#41; - instantiated session factory
    
    &#91;22/08/05 12&#58;59&#58;44&#58;417 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;44,355  INFO &#91;Servlet.Engine.Transports&#58;10&#93; &#40;SessionFactoryImpl.java&#58;379&#41; - Checking 0 named queries
    
    &#91;22/08/05 12&#58;59&#58;45&#58;323 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;45,323 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;SessionImpl.java&#58;250&#41; - opened session at timestamp&#58; 4606894019629056
    
    &#91;22/08/05 12&#58;59&#58;45&#58;339 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;45,339 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;DefaultLoadEventListener.java&#58;143&#41; - loading entity&#58; &#91;com.eg.model.Bookkeeping#component&#91;request,seqNo&#93;&#123;seqNo=1, request=com.eg.model.Request#6175&#125;&#93;
    
    &#91;22/08/05 12&#58;59&#58;45&#58;339 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;45,339 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;DefaultLoadEventListener.java&#58;216&#41; - creating new proxy for entity
    
    &#91;22/08/05 12&#58;59&#58;45&#58;355 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;45,355 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;JDBCContext.java&#58;322&#41; - after autocommit
    
    &#91;22/08/05 12&#58;59&#58;45&#58;355 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;45,355 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;SessionImpl.java&#58;403&#41; - after transaction completion
    
    &#91;22/08/05 12&#58;59&#58;45&#58;355 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;45,355 DEBUG &#91;Servlet.Engine.Transports&#58;10&#93; &#40;SessionImpl.java&#58;269&#41; - closing session
    
    &#91;22/08/05 12&#58;59&#58;45&#58;370 EDT&#93; 55d2b2f9 SystemOut     U 2005-08-22 12&#58;59&#58;45,355 ERROR &#91;Servlet.Engine.Transports&#58;10&#93; &#40;LazyInitializationException.java&#58;19&#41; - could not initialize proxy - the owning Session was closed
    org.hibernate.LazyInitializationException&#58; could not initialize proxy - the owning Session was closed
    	at org.hibernate.proxy.AbstractLazyInitializer.initialize&#40;AbstractLazyInitializer.java&#58;53&#41;
    	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation&#40;AbstractLazyInitializer.java&#58;84&#41;
    	at org.hibernate.proxy.CGLIBLazyInitializer.intercept&#40;CGLIBLazyInitializer.java&#58;134&#41;
    	at com.eg.model.Bookkeeping$$EnhancerByCGLIB$$937253be.getSettlementDate&#40;<generated>&#41;
    	at com.eg.dao.impl.BookkeepingDaoImpl.findBookkeepingById&#40;BookkeepingDaoImpl.java&#58;23&#41;
    	at com.eg.transaction.TestTransactionBeanBean.testSpringDaoAccess&#40;TestTransactionBeanBean.java&#58;51&#41;
    	at com.eg.transaction.EJSRemoteStatelessTestTransactionBean_80af6024.testSpringDaoAccess&#40;EJSRemoteStatelessTestTransactionBean_80af6024.java&#58;22&#41;
    	at com.eg.transaction._TestTransactionBean_Stub.testSpringDaoAccess&#40;_TestTransactionBean_Stub.java&#58;254&#41;
    	at java.lang.reflect.Method.invoke&#40;Native Method&#41;
    	at com.ibm.etools.utc.model.ReflectionMethodModel.invoke&#40;ReflectionMethodModel.java&#58;68&#41;
    	at com.ibm.etools.utc.servlet.InvokeServlet.invoke&#40;InvokeServlet.java&#58;110&#41;
    	at com.ibm.etools.utc.servlet.InvokeServlet.doPost&#40;InvokeServlet.java&#58;366&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;760&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;853&#41;
    	at com.ibm.servlet.engine.webapp.StrictServletInstance.doService&#40;ServletManager.java&#58;827&#41;
    	at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service&#40;StrictLifecycleServlet.java&#58;167&#41;
    	at com.ibm.servlet.engine.webapp.IdleServletState.service&#40;StrictLifecycleServlet.java&#58;297&#41;
    	at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service&#40;StrictLifecycleServlet.java&#58;110&#41;
    	at com.ibm.servlet.engine.webapp.ServletInstance.service&#40;ServletManager.java&#58;472&#41;
    	at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch&#40;ServletManager.java&#58;1012&#41;
    	at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch&#40;ServletManager.java&#58;913&#41;
    	at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch&#40;WebAppRequestDispatcher.java&#58;721&#41;
    	at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch&#40;WebAppRequestDispatcher.java&#58;374&#41;
    	at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward&#40;WebAppRequestDispatcher.java&#58;118&#41;
    	at com.ibm.servlet.engine.srt.WebAppInvoker.doForward&#40;WebAppInvoker.java&#58;134&#41;
    	at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook&#40;WebAppInvoker.java&#58;239&#41;
    	at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation&#40;CachedInvocation.java&#58;67&#41;
    	at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke&#40;CacheableInvocationContext.java&#58;106&#41;
    	at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI&#40;ServletRequestProcessor.java&#58;154&#41;
    	at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service&#40;OSEListener.java&#58;317&#41;
    	at com.ibm.servlet.engine.http11.HttpConnection.handleRequest&#40;HttpConnection.java&#58;60&#41;
    	at com.ibm.ws.http.HttpConnection.readAndHandleRequest&#40;HttpConnection.java&#58;477&#41;
    	at com.ibm.ws.http.HttpConnection.run&#40;HttpConnection.java&#58;351&#41;
    	at com.ibm.ws.util.CachedThread.run&#40;ThreadPool.java&#58;144&#41;
    
    &#91;22/08/05 12&#58;59&#58;45&#58;370 EDT&#93; 55d2b2f9 ExceptionUtil X CNTR0020E&#58; Non-application exception occurred while processing method testSpringDaoAccess on bean BeanId&#40;EJBApplicationProject#EJBApplicationProjectEJB.jar#TestTransactionBean, null&#41;&#58; org.hibernate.LazyInitializationException&#58; could not initialize proxy - the owning Session was closed
    	at org.hibernate.proxy.AbstractLazyInitializer.initialize&#40;AbstractLazyInitializer.java&#58;53&#41;
    	at org.hibernate.proxy.AbstractLazyInitializer.getImplementation&#40;AbstractLazyInitializer.java&#58;84&#41;
    	at org.hibernate.proxy.CGLIBLazyInitializer.intercept&#40;CGLIBLazyInitializer.java&#58;134&#41;
    	at com.eg.model.Bookkeeping$$EnhancerByCGLIB$$937253be.getSettlementDate&#40;<generated>&#41;
    	at com.eg.dao.impl.BookkeepingDaoImpl.findBookkeepingById&#40;BookkeepingDaoImpl.java&#58;23&#41;
    	at com.eg.transaction.TestTransactionBeanBean.testSpringDaoAccess&#40;TestTransactionBeanBean.java&#58;51&#41;
    	at com.eg.transaction.EJSRemoteStatelessTestTransactionBean_80af6024.testSpringDaoAccess&#40;EJSRemoteStatelessTestTransactionBean_80af6024.java&#58;22&#41;
    	at com.eg.transaction._TestTransactionBean_Stub.testSpringDaoAccess&#40;_TestTransactionBean_Stub.java&#58;254&#41;
    	at java.lang.reflect.Method.invoke&#40;Native Method&#41;
    	at com.ibm.etools.utc.model.ReflectionMethodModel.invoke&#40;ReflectionMethodModel.java&#58;68&#41;
    	at com.ibm.etools.utc.servlet.InvokeServlet.invoke&#40;InvokeServlet.java&#58;110&#41;
    	at com.ibm.etools.utc.servlet.InvokeServlet.doPost&#40;InvokeServlet.java&#58;366&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;760&#41;
    	at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;853&#41;
    	at com.ibm.servlet.engine.webapp.StrictServletInstance.doService&#40;ServletManager.java&#58;827&#41;
    	at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service&#40;StrictLifecycleServlet.java&#58;167&#41;
    	at com.ibm.servlet.engine.webapp.IdleServletState.service&#40;StrictLifecycleServlet.java&#58;297&#41;
    	at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service&#40;StrictLifecycleServlet.java&#58;110&#41;
    	at com.ibm.servlet.engine.webapp.ServletInstance.service&#40;ServletManager.java&#58;472&#41;
    	at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch&#40;ServletManager.java&#58;1012&#41;
    	at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch&#40;ServletManager.java&#58;913&#41;
    	at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch&#40;WebAppRequestDispatcher.java&#58;721&#41;
    	at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch&#40;WebAppRequestDispatcher.java&#58;374&#41;
    	at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward&#40;WebAppRequestDispatcher.java&#58;118&#41;
    	at com.ibm.servlet.engine.srt.WebAppInvoker.doForward&#40;WebAppInvoker.java&#58;134&#41;
    	at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook&#40;WebAppInvoker.java&#58;239&#41;
    	at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation&#40;CachedInvocation.java&#58;67&#41;
    	at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke&#40;CacheableInvocationContext.java&#58;106&#41;
    	at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI&#40;ServletRequestProcessor.java&#58;154&#41;
    	at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service&#40;OSEListener.java&#58;317&#41;
    	at com.ibm.servlet.engine.http11.HttpConnection.handleRequest&#40;HttpConnection.java&#58;60&#41;
    	at com.ibm.ws.http.HttpConnection.readAndHandleRequest&#40;HttpConnection.java&#58;477&#41;
    	at com.ibm.ws.http.HttpConnection.run&#40;HttpConnection.java&#58;351&#41;
    	at com.ibm.ws.util.CachedThread.run&#40;ThreadPool.java&#58;144&#41;

    Can anyone help me please?


    Etienne.

  2. #2
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    54

    Default

    Sorry,

    my environment is non XA, not non-JTA. JTA is the interface for j2ee, I was mixing the 2 termes. Sorry for the confusion. I finnaly found the way to bind the Sessino to the transaction and found why the current JTA Transaction was not found: i have to set the org.hibernate.transaction.WebSphereTransactionMana gerLookup in Hibernate Config and use this configuration:

    Code:
    	<!--Use an AOP interceptor to attach the Hibernate session to CMT for session-per-
    		- transaction scoping.  This way one hibernate session will live with the transaction.   -->
    	<bean id="myHibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    
    
    
    	<bean id="grd-bookkeepingDao" class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="proxyInterfaces">
    			<value>com.eg.dao.BookkeepingDao</value>
    		</property>
    		<property name="interceptorNames">
    			<list>
    				<value>myHibernateInterceptor</value>
    				<value>grd-bookkeepingDaoTarget</value>
    			</list>
    		</property>
    	</bean>
    
    
    	<!-- grd-bookkeeping DAO -->
    	<bean id="grd-bookkeepingDaoTarget" class="com.eg.dao.impl.BookkeepingDaoImpl">
    		<property name="sessionFactory">
    			<ref bean="sessionFactory" />
    		</property>
    	</bean>
    I will make more test and understand a biut more how all this work. I just hope everything will work fine now.

    I saw in another mail that you do not have to declare the TransactionProxy because Spring will find the transaction automatically in a JTA environement. But is there a way to force the transaction attribute to PROPAGATION_MANDATORY?

    I dont want Spring to create a Transaction if there's no transaction. Also, how can I be sure that Spring will never commit the current transaction?

    Thanks.

    Etienne.

  3. #3
    Join Date
    Aug 2004
    Location
    Linz, Austria
    Posts
    391

    Default

    As long as you're running within an actual EJB CMT transaction and you specified an appropriate TransactionManagerLookup, each Session should automatically be scoped per transaction. You shouldn't even need a HibernateInterceptor definition.

    To access such a transaction Session, you can either use Spring's HibernateTemplate or Hibernate3's "SessionFactory.getCurrentSession()" method. Both will automatically expose a transaction-scoped Session for the current transaction.

    Juergen

Similar Threads

  1. Unit testing with JOTM and JtaTransactionManager
    By lalle in forum Architecture
    Replies: 1
    Last Post: Oct 15th, 2005, 09:05 AM
  2. Replies: 0
    Last Post: Jun 6th, 2005, 06:22 AM
  3. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM

Posting Permissions

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