Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Mixing JDBC and Hibernate in same DAO

  1. #1
    Join Date
    Aug 2004
    Posts
    105

    Default Mixing JDBC and Hibernate in same DAO

    Hello,
    I know that this is old problem and many threads ahave laready discussed that i have read all that but facing a problem. I am extending my DAO from HibernateDAOSupport and have exposed the dataSource property. Everything is working fine but i am getting connection close exception. The stack trace is as (using spring 1.1 Rc 2, Hibernate 2.1.6, JBoss 3.2.3, I am using SLSB + EJB CMT.
    I am getting following exception of connection not closing

    12:39:37,071 INFO [CachedConnectionManager] Closing a connection for you. Please close them yourself: org.jboss.resource.adapter.jdbc.WrappedConnection@ 17e53c9

    java.lang.Exception: STACKTRACE

    at org.jboss.resource.connectionmanager.CachedConnect ionManager.registerConnection(CachedConnectionMana ger.java:282)

    at org.jboss.resource.connectionmanager.BaseConnectio nManager2.allocateConnection(BaseConnectionManager 2.java:506)

    at org.jboss.resource.connectionmanager.BaseConnectio nManager2$ConnectionManagerProxy.allocateConnectio n(BaseConnectionManager2.java:814)

    at org.jboss.resource.adapter.jdbc.WrapperDataSource. getConnection(WrapperDataSource.java:102)

    at org.springframework.orm.hibernate.LocalDataSourceC onnectionProvider.getConnection(LocalDataSourceCon nectionProvider.java:66)

    at net.sf.hibernate.impl.BatcherImpl.openConnection(B atcherImpl.java:286)

    at net.sf.hibernate.impl.SessionImpl.connect(SessionI mpl.java:3326)

    at net.sf.hibernate.impl.SessionImpl.connection(Sessi onImpl.java:3286)

    at net.sf.hibernate.impl.BatcherImpl.prepareStatement (BatcherImpl.java:61)

    at net.sf.hibernate.impl.BatcherImpl.prepareStatement (BatcherImpl.java:56)

    at net.sf.hibernate.impl.BatcherImpl.prepareBatchStat ement(BatcherImpl.java:109)

    at net.sf.hibernate.persister.EntityPersister.insert( EntityPersister.java:460)

    at net.sf.hibernate.persister.EntityPersister.insert( EntityPersister.java:439)

    at net.sf.hibernate.impl.ScheduledInsertion.execute(S cheduledInsertion.java:29)

    at net.sf.hibernate.impl.SessionImpl.executeAll(Sessi onImpl.java:2418)

    at net.sf.hibernate.impl.SessionImpl.execute(SessionI mpl.java:2371)

    at net.sf.hibernate.impl.SessionImpl.flush(SessionImp l.java:2240)

    at com.sequelsys.server.common.dao.TestDAO$1.doInHibe rnate(TestDAO.java:81)

    at org.springframework.orm.hibernate.HibernateTemplat e.execute(HibernateTemplate.java:176)

    at com.sequelsys.server.common.dao.TestDAO.insert(Tes tDAO.java:101)

    at com.shoaib.ejb.session.TestingTransactionSessionBe an.save(TestingTransactionSessionBean.java:38)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)

    at java.lang.reflect.Method.invoke(Method.java:324)

    at org.jboss.ejb.StatelessSessionContainer$ContainerI nterceptor.invoke(StatelessSessionContainer.java:6 83)

    at org.jboss.resource.connectionmanager.CachedConnect ionInterceptor.invoke(CachedConnectionInterceptor. java:185)

    at org.jboss.ejb.plugins.StatelessSessionInstanceInte rceptor.invoke(StatelessSessionInstanceInterceptor .java:72)

    at org.jboss.ejb.plugins.AbstractTxInterceptor.invoke Next(AbstractTxInterceptor.java:84)

    at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTran sactions(TxInterceptorCMT.java:267)

    at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxIn terceptorCMT.java:128)

    at org.jboss.ejb.plugins.SecurityInterceptor.invoke(S ecurityInterceptor.java:118)

    at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInt erceptor.java:191)

    at org.jboss.ejb.plugins.ProxyFactoryFinderIntercepto r.invoke(ProxyFactoryFinderInterceptor.java:122)

    at org.jboss.ejb.StatelessSessionContainer.internalIn voke(StatelessSessionContainer.java:331)

    at org.jboss.ejb.Container.invoke(Container.java:700)

    at org.jboss.ejb.plugins.local.BaseLocalProxyFactory. invoke(BaseLocalProxyFactory.java:375)

    at org.jboss.ejb.plugins.local.StatelessSessionProxy. invoke(StatelessSessionProxy.java:83)

    at $Proxy35.save(Unknown Source)

    at com.shoaib.ejb.session.facade.session.ApplicationF acadeSessionBean.saveTest(ApplicationFacadeSession Bean.java:62)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)

    at java.lang.reflect.Method.invoke(Method.java:324)

    at org.jboss.ejb.StatelessSessionContainer$ContainerI nterceptor.invoke(StatelessSessionContainer.java:6 83)

    at org.jboss.resource.connectionmanager.CachedConnect ionInterceptor.invoke(CachedConnectionInterceptor. java:185)

    at org.jboss.ejb.plugins.StatelessSessionInstanceInte rceptor.invoke(StatelessSessionInstanceInterceptor .java:72)

    at org.jboss.ejb.plugins.AbstractTxInterceptor.invoke Next(AbstractTxInterceptor.java:84)

    at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTran sactions(TxInterceptorCMT.java:267)

    at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxIn terceptorCMT.java:128)

    at org.jboss.ejb.plugins.SecurityInterceptor.invoke(S ecurityInterceptor.java:118)

    at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInt erceptor.java:191)

    at org.jboss.ejb.plugins.ProxyFactoryFinderIntercepto r.invoke(ProxyFactoryFinderInterceptor.java:122)

    at org.jboss.ejb.StatelessSessionContainer.internalIn voke(StatelessSessionContainer.java:331)

    at org.jboss.ejb.Container.invoke(Container.java:700)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)

    at java.lang.reflect.Method.invoke(Method.java:324)

    at org.jboss.mx.capability.ReflectedMBeanDispatcher.i nvoke(ReflectedMBeanDispatcher.java:284)

    at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanSe rverImpl.java:546)

    at org.jboss.invocation.jrmp.server.JRMPInvoker.invok e(JRMPInvoker.java:367)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)

    at java.lang.reflect.Method.invoke(Method.java:324)

    at sun.rmi.server.UnicastServerRef.dispatch(UnicastSe rverRef.java:261)

    at sun.rmi.transport.Transport$1.run(Transport.java:1 48)

    at java.security.AccessController.doPrivileged(Native Method)

    at sun.rmi.transport.Transport.serviceCall(Transport. java:144)

    at sun.rmi.transport.tcp.TCPTransport.handleMessages( TCPTransport.java:460)

    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandl er.run(TCPTransport.java:701)

    at java.lang.Thread.run(Thread.java:534)


    My DAO is as follows.
    -------------
    DAO
    ---------------
    Code:
    package com.sequelsys.server.common.dao;
    
    import javax.sql.*;
    
    import org.springframework.jdbc.core.*;
    import org.springframework.jdbc.oracle.*;
    import org.springframework.orm.hibernate.*;
    import airline.hibernate.*;
    import net.sf.hibernate.*;
    import org.springframework.orm.hibernate.support.HibernateDaoSupport;
    
    public class TestDAO extends HibernateDaoSupport{
      private DataSource dataSource;
      public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
      }
    
      public DataSource getDataSource() {
        return dataSource;
      }
    
      public TestDAO() {
      }
     
     
      public void insert()
     {
    
       HibernateCallback hibernateCallback = new HibernateCallback() {
         public Object doInHibernate(Session session) {
           try {
             session.flush();
           }
           catch (HibernateException ex) {
             ex.printStackTrace();
           }
           return null;
         }
    
       };
       Test test = new Test();
             test.setId(new Long("18"));
             test.setName("BS");
       this.getHibernateTemplate().save(test);
       Test test2 = new Test();
             test2.setId(new Long("19"));
             test2.setName("BS");
       this.getHibernateTemplate().save(test2);
       this.getHibernateTemplate().execute(hibernateCallback);    // To flush the session so that jdbc can see this
       JdbcTemplate jdbcTemplate = new JdbcTemplate(this.dataSource);
       System.out.println((jdbcTemplate.queryForInt("SELECT COUNT(*) FROM test")));
       System.out.println("Running Update" );
       jdbcTemplate.update("Update Test set name='Benzi' where id = 18");
       jdbcTemplate.update("delete from test where id = 18");
       System.out.println((jdbcTemplate.queryForInt("SELECT COUNT(*) FROM test")));
       System.out.println("Ran Update" ); 
      
     }
    }

    and applicationContetx is as follows
    ---------
    Code:
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "file&#58;///C&#58;/jboss-3.2.3/server/default/lib/spring-beans.dtd">
    <!--Application context definition for EMR Relase 1.0 maintained by Shoaib on Hibernate.--><beans>
          <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    		<property name="jndiName"><value>java&#58;/OracleDS</value></property>
    	</bean>
    
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    		<property name="dataSource"><ref local="dataSource" /></property>
    		<!-- Mapping Resources for Hibernate Session Factory &#40;Specify the hbm.xml to be used here&#41; -->
                    <property name="mappingResources">
                      <list>
                        <value>airline/hibernate/Test.hbm.xml</value>
                      </list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    			  <prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
                              <prop key="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</prop>
                              <prop key="hibernate.transaction.manager_lookup_class">net.sf.hibernate.transaction.JBossTransactionManagerLookup</prop>
                              <prop key="hibernate.show_sql">true</prop>
                              <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                            </props>
    		</property>
    	</bean>
    	<!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
    	<bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
    		<property name="sessionFactory"><ref local="sessionFactory" /></property>
                   <property name="dataSource"><ref local="dataSource" /></property>
    	</bean>
            <!-- Refereces of DAOs to be used by Session beans-->
          <bean id="testDAO" class="com.sequelsys.server.common.dao.TestDAO" autowire="byName" />
    </beans>
    If i only use one of them (hibernatetemplate or jdbctemplate) i get no exception at all. Though this exception is coming all other things are working kindly help me asap.
    Regards,
    Shoaib Akhtar

  2. #2
    Join Date
    Aug 2004
    Posts
    105

    Default Re,

    Hello,
    I just want to add that i also tried one class from Rod's Reply to similiar question on the previos forum but everything is fine but i am getting connection closing exception. Just want to mention i am using Oracle 9i,JBoss 3.2.3, Hibernate 2.1.6 and SLSB + EJB+CMT. Please also verify this class should i use that or not?

    Code:
    package org.springframework.jdbc.oracle;
    
    import javax.sql.*;
    
    import org.apache.commons.logging.*;
    import org.springframework.jdbc.core.*;
    import org.springframework.orm.hibernate.*;
    import net.sf.hibernate.*;
    
    public abstract class JdbcHibernateDaoSupport &#123;
      protected final Log logger = LogFactory.getLog&#40;getClass&#40;&#41;&#41;;
      private HibernateTemplate hibernateTemplate;
      private JdbcTemplate jdbcTemplate;
      private DataSource dataSource;
      private SessionFactory sessionFactory;
      public void setDataSource&#40;DataSource dataSource&#41; &#123;
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate&#40;this.dataSource&#41;;
      &#125;
    
      public void setSessionFactory&#40;SessionFactory sessionFactory&#41; &#123;
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate&#40;this.sessionFactory&#41;;
      &#125;
    
      public HibernateTemplate getHibernateTemplate&#40;&#41; &#123;
        return hibernateTemplate;
      &#125;
    
      public JdbcTemplate getJdbcTemplate&#40;&#41; &#123;
        return jdbcTemplate;
      &#125;
    
      public SessionFactory getSessionFactory&#40;&#41; &#123;
        return sessionFactory;
      &#125;
    
      public DataSource getDataSource&#40;&#41; &#123;
        return dataSource;
      &#125;
    
      public void flushHibernate&#40;&#41; &#123;
        HibernateCallback hibernateCallback = new HibernateCallback&#40;&#41; &#123;
          public Object doInHibernate&#40;Session session&#41; &#123;
            try &#123;
              session.flush&#40;&#41;;
            &#125;
            catch &#40;HibernateException ex&#41; &#123;
              ex.printStackTrace&#40;&#41;;
            &#125;
            return null;
          &#125;
        &#125;;
       hibernateTemplate.execute&#40;hibernateCallback&#41;;
      &#125;
    
    &#125;
    Regards,
    Shoaib Akhtar

  3. #3
    Join Date
    Aug 2004
    Posts
    105

    Default Re

    Hello,
    I think it should be a bug in spring. The possible cause is that when that both jdbcTemplate and hibernateTempalte have dependencies on the connection and both have some kmind of deadlock due to which they cannot close connection. It is connnection leakage problem beacuse i do not tjink that using jdbc and hibernate in one method is wrong as requirementts may enforce that. A suggestion is that Spring framework may provide a super class itself which combines HibernateDAOSupport and JDBCDAOSupport such as JdbcHibernateDAOSupport it will be very convinioent for user. In the meantime please help me ragrding this connection leakage problem.
    Regards,
    Shoaib Akhtar

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    I used to get this myself in my old app.

    I spent a lot of time (hours) tracing through both Spring and even JBoss code to try to find out what was going on, and as far as I can tell JBoss's connection manager is simply confused in the face of usage of the connection from multiple sources. Spring properly obtained and released all resources. In the case of the message about releasing the connection yourself, Spring did in fact already release the connection. There is another case where JBoss complained about a connection being unknown on a release call. Same deal here, it was a valid connection. JBoss is simply buggy, as far as I can tell.

    If you can provide a test case that shows this behaviour happening, and ideally put it up on Jira, then I can spend some more time trying to duplicate it. The ejbtest integration sample in our CVS unfortunately doesn't display this behaviour with JBoss, so I don't have any way to duplicate this right now...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  5. #5
    Join Date
    Aug 2004
    Posts
    105

    Default Re,

    Hello,
    Thanks Colin for your answer i will put up all the things u mentioned possibly tommorrow. In the meantime please tell me that is doing so is right?
    Code:
    package org.springframework.jdbc.oracle;
    
    import javax.sql.*;
    
    import org.apache.commons.logging.*;
    import org.springframework.jdbc.core.*;
    import org.springframework.orm.hibernate.*;
    import net.sf.hibernate.*;
    
    public abstract class JdbcHibernateDaoSupport &#123;
      protected final Log logger = LogFactory.getLog&#40;getClass&#40;&#41;&#41;;
      private HibernateTemplate hibernateTemplate;
      private JdbcTemplate jdbcTemplate;
      private DataSource dataSource;
      private SessionFactory sessionFactory;
      public void setDataSource&#40;DataSource dataSource&#41; &#123;
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate&#40;this.dataSource&#41;;
      &#125;
    
      public void setSessionFactory&#40;SessionFactory sessionFactory&#41; &#123;
        this.sessionFactory = sessionFactory;
        this.hibernateTemplate = new HibernateTemplate&#40;this.sessionFactory&#41;;
      &#125;
    
      public HibernateTemplate getHibernateTemplate&#40;&#41; &#123;
        return hibernateTemplate;
      &#125;
    
      public JdbcTemplate getJdbcTemplate&#40;&#41; &#123;
        return jdbcTemplate;
      &#125;
    
      public SessionFactory getSessionFactory&#40;&#41; &#123;
        return sessionFactory;
      &#125;
    
      public DataSource getDataSource&#40;&#41; &#123;
        return dataSource;
      &#125;
       /** This flushes the hibernate session associated with this transaction so changes can be viewed by jdbc code **/
      public void flushHibernate&#40;&#41; &#123;
        HibernateCallback hibernateCallback = new HibernateCallback&#40;&#41; &#123;
          public Object doInHibernate&#40;Session session&#41; &#123;
            try &#123;
              session.flush&#40;&#41;;
            &#125;
            catch &#40;HibernateException ex&#41; &#123;
              ex.printStackTrace&#40;&#41;;
            &#125;
            return null;
          &#125;
        &#125;;
       hibernateTemplate.execute&#40;hibernateCallback&#41;;
      &#125;
    
    &#125;
    1- Is the above code right or not? And i use it in production? and any suggestions if i am missing sth there (want a super class for Hibernate + JDbc together)
    2- Second point i want to make is that i think that hibernateTempalte should have flush method delegating to Session.flush (The session currently bound with the running transaction) beacuse flushing the hibernate session is must if we want to show the results to jdbc. I know traht their is FlushMode.EAGEr available but i think so taht using an explict flush will be better option as sometimes due to triggers we have to refresh the prevoiusly saved object so flush may help their as well.
    3-One thing more in your sample in autobuild folder of CVS ditribution you have used
    Code:
    this.hibernateTemplate = new HibernateTemplate&#40;this.sessionFactory,false&#41;;
    Why it is necessary? And whenevr i do taht i get exception that creating session is not allowed so please make it clear.

    4- other thing i want to told you about the problem is that i face no connection leakage exception when i only use Hibernate tempalte or jdbc template.... it is only when i use both of them in one method ( by extending HibernateDAOSupport and using jdbcTempalte or viceverca or by using another class such as JdbcHibernateDAOSupport but mixing tem in my case is neccesary so your test case may be to use both jdbcTempalet and HibernateTempalte in one method of a class with extending from HibernateDAOSupport and injecting dataSource and making jdbcTempalte using taht data Source and using it in the same method or vice versa or using the class JdbcHibernateDAOSupport)

    Waiting
    Regards,
    Shoaib Akhtar

  6. #6
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    The code looks generally ok...

    With regards to your question about why my sample code does:

    this.hibernateTemplate = new HibernateTemplate(this.sessionFactory,false)

    this is specifically because I want to catch instances where the template is not being run in a transaction like it should be. With the default allowCreate flag setting of true, if there is no thread-bound Hibernate Session already available, then the template will create one then and there, and Spring code will even ensure that it is bound to any wrappig transaction if it exists. The problem is that it also means that if there is no wrapping transaction, you end up running non-transactionally, and have a new Session for every use of HibernateTemplate, not what most people want. I simply prefer to use either HibernateTransactionManager or JTATransactionManager+HibernateInterceptor, both of which should ensure that a thread bound Session is already created ahead of time with the transaciton, and then not allow the template to create the Session itself. Then an error such as the one you are getting is an indication that in fact the code is not running inside a transaction, as there is no thread bound session available.

    This is assuming of course that your Hibernate code is always going to run in a transaction, which is a good thing I think...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  7. #7
    Join Date
    Aug 2004
    Posts
    105

    Default Re,

    Hello,
    Thanks Colin for reply. I tested my code by many ways and it appears to run in transaction. I did not get one point, i am using HibernateTransactionManger
    and hibernate is using jta as you can see in above xml file, so in this case should i pass false in base class to HibernateTempalte Constructor or not. Secondly can u tell the other way JTaTransactionManager + HibernateInterceptor how to do that. And thirdly you did not answer my second question about the flush method. As i told you , u can test the case i mentioned easily by using my base class (JdbcHibernateDAOSupprt) and use both jdbc and hibernate in single method. Please help me asap i am very thankful to you
    Regards,
    Shoaib Akhtar

  8. #8
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    If you are using HibernateTransactionManager, then along with creating a transaction, it will have ensured that a Session is bound to the current thread and trasnaction. If your allowCreate flag on the template is off with this config, and you get an exception, it means you are not in a Spring managed transaction, end of story, as a transaction would have created a Session already...

    To use JTATransactionManager+HibernateInterceptor, just specify JTATransactionManager as the Spring transaction manager, and specify HibernateIntercetor as a postInterceptor. The HibernateInterceptor just ensures a Hibernate Session is bound to the thread/transaction eagerly (same as if you used HibernateTransactionManager directly). Again, if you use JTATransactionManager+HibernateInterceptor, you can turn off the allowCreate flag in the template, since the transaction should have caused the Session to be created. This is all assuming your Hibernate code will always run transactionally.

    If you use EJBs and CMT (Container Managed Transactions) with JTA, but no Spring Transactions, then you will have to leave the allowCreate flag set at true, since there will be no other Spring transaction code to tie eager Session creation to.

    As for the flush, it could be added to the template. However, I'm not so sure it's an appropriate method, given the fact that it is almost always associated with other code. While it would be indeed useful when used with an existing thread-bound Session, it would be completely useless (superfluous rather) on the other hand in the case where there is no already existing thread bound session (or tx to bind new session to), since in that case the template will create and destroy the session for each operation, and the destroy will force a flush anyways. Admittedly, most poeple do not use the template that way...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  9. #9
    Join Date
    Aug 2004
    Posts
    105

    Default Re

    Hello Colin,
    Once again many many thanks for your reply, that is so kind of you. I am using EJB + CMT .I understood the flush part. But a bit confused about passing true or false to sessionfactory.create method as i am using HibernateTransactionManager, and in hibernate properties i am pointing towards JTA. My application-context.xml is as follows. In this case should i pass allowcreate false or true.
    Code:
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "file&#58;///C&#58;/jboss-3.2.3/server/default/lib/spring-beans.dtd">
    <!--Application context definition for EMR Relase 1.0 maintained by Shoaib on Hibernate.--><beans>
          <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
          <property name="jndiName"><value>java&#58;/OracleDS</value></property>
       </bean>
    
       <!-- Hibernate SessionFactory -->
       <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
          <property name="dataSource"><ref local="dataSource" /></property>
          <!-- Mapping Resources for Hibernate Session Factory &#40;Specify the hbm.xml to be used here&#41; -->
                    <property name="mappingResources">
                      <list>
                        <value>airline/hibernate/Test.hbm.xml</value>
                      </list>
          </property>
          <property name="hibernateProperties">
             <props>
               <prop key="hibernate.dialect">net.sf.hibernate.dialect.Oracle9Dialect</prop>
                              <prop key="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</prop>
                              <prop key="hibernate.transaction.manager_lookup_class">net.sf.hibernate.transaction.JBossTransactionManagerLookup</prop>
                              <prop key="hibernate.show_sql">true</prop>
                              <prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
                            </props>
          </property>
       </bean>
       <!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
       <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
          <property name="sessionFactory"><ref local="sessionFactory" /></property>
                   <property name="dataSource"><ref local="dataSource" /></property>
       </bean>
            <!-- Refereces of DAOs to be used by Session beans-->
          <bean id="testDAO" class="com.sequelsys.server.common.dao.TestDAO" autowire="byName" />
    </beans>
    I will be very grateful to you kindly reply for the above configuration
    Regards,
    Shoaib Akhtar

  10. #10
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Shoaib,

    The javadocs for the allowCreate flag are explicit, and I have already told you twice in this thread. If you set the flag to false, then the Session must already exist on the thread as a result of something else putting it there. If you know for sure that you will always be in a spring transaction managed by HibernateTransactionManager or JTATransacitonMaanger+HibernateInterceptor, those two will ensure that in fact a Session is pre-bound. In this case, setting the flag to false will allow you to catch cases where you think you are running in a spring transaction, but are not, because you haven't wrapped your services properly or for whatever reason.

    If in your case you are getting the exception even though you are using HibernateTransactionManger, then you are not in a spring managed transaction. But you mentioned using CMT, so if you are calling this code from a CMT managed EJB, then you are probably in a CMT transaction.

    Nobody is forcing you to use both CMT and Spring managed transacitons by the way. If you just want to use CMT, don't use a Spring transaction manager at all, make sure that the hibernate JTALookup is set up properly, and leave the allowCreate flag as set to true so that the first time a session is needed by the template it can create it, which will then bind to the JTA transaction and thread. Alternately, you can stop using CMT, and just use Spring transactions. Using CMT+Spring tx can be useful to mix and match transactional EJBs and service objects, but it complicates things if you don't need it.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Replies: 2
    Last Post: Oct 14th, 2005, 08:40 AM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Replies: 0
    Last Post: Apr 6th, 2005, 08:24 AM
  4. Mixing jdbc and hibernate in same trx
    By hucmuc in forum Data
    Replies: 1
    Last Post: Jan 23rd, 2005, 01:59 PM
  5. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 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
  •