-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: save(obj) does not commit the JDBC transaction
PostPosted: Thu Mar 31, 2011 4:56 am 
Newbie

Joined: Thu Mar 31, 2011 4:37 am
Posts: 11
Dear,

I'm using an application server (wasce) connection pool trought Hibernate to connect an Oracle database.
When I do a "getSession().save(Obj)" , the object is saved in my Hibernate session, but it is not saved in the database even with a flush.
To synchronize the database, i had to add a "getSession().connection().setAutoCommit(true);" and now, it works.
However, Session.connection() is now deprecated and I think it's not the good way to configure hibernate properly anyway..

Could you tell me a good alternative to configure my connection pool ?

Here is my config.. (PS: I'm using spring)

hibernate.hbm.xml :
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
     <property name="connection.datasource">java:comp/env/jdbc/oracleJNDI</property>
        <property name="default_schema">MYFIRSTSCHEMA</property>
        <property name="current_session_context_class">thread</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>   
       +MAPPING...
    </session-factory>
</hibernate-configuration>

springmvc-servlet.xml
Code:
<!--  DB Transaction definition -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="transactionProxy" abstract="true"
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
   <property name="transactionManager">
      <ref bean="transactionManager"/>
   </property>
   <property name="transactionAttributes">
      <props>
         <prop key="insert*">PROPAGATION_REQUIRED</prop>
         <prop key="update*">PROPAGATION_REQUIRED</prop>
         <prop key="save*">PROPAGATION_REQUIRED</prop>
         <prop key="*">PROPAGATION_REQUIRED, readOnly</prop>
      </props>
   </property>
</bean>
<!-- sessionFactory  -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="configLocation">
         <value>classpath:hibernate.cfg.xml</value>
      </property>
      <!-- <property name="mappingLocations">
         <list>
            <value>classpath:hibernate.cfg.xml</value>
            <value>classpath:hibernate.minterne.cfg.xml</value>
         </list>
      </property>-->
      <property  name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
      </property>
   </bean>
  <!--  END DB Transaction definition -->


Thanks a lot.


Top
 Profile  
 
 Post subject: Re: save(obj) does not commit the JDBC transaction
PostPosted: Thu Mar 31, 2011 5:22 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you have to configure the transaction manager to be used by the SessionFactory, I'm not a Spring expert but it seems you configured a TM on Spring, but didn't set the same in the Hibernate configuration.
You can enable SQL logging, you'll see that when you flush Hibernate is definitely performing the insert/update statements, but as you said in the subject it doesn't seem to commit the database transaction, so if you connect on another console you won't see the inserted data.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: save(obj) does not commit the JDBC transaction
PostPosted: Thu Mar 31, 2011 5:59 am 
Newbie

Joined: Thu Mar 31, 2011 4:37 am
Posts: 11
Thanks for your reply.

But I don't get it..
Here is a part of my DAO Code :
Code:

public class GenericDaoImpl<T, PK extends Serializable> implements GenericDao<T, PK> { 

    private SessionFactory sessionFactory; 
    private Class<T> type; 

  @SuppressWarnings("unchecked")
  @Override
   public PK saveObj(T o) { 
       PK key = (PK) getSession().save(o);
       return key;
    }

public Session getSession() { 
        boolean allowCreate = true; 
        Session sess = SessionFactoryUtils.getSession(sessionFactory, allowCreate);
       /* try {
         sess.connection().setAutoCommit(true);
      } catch (HibernateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }*/
      return sess;
    } 

   

    public void setSessionFactory(SessionFactory sessionFactory) { 
        this.sessionFactory = sessionFactory;
    }
}

Spring injects the sessionFactory in this DAO, as you can see in the springmvc-servlet.xml, the sessionFactory and the transactionManager are linked. So where is the problem ?
As soon as i remove the comment marks (sess.connection().setAutoCommit(true);), the database is synchronized.


Top
 Profile  
 
 Post subject: Re: save(obj) does not commit the JDBC transaction
PostPosted: Thu Mar 31, 2011 6:52 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
the sessionFactory and the transactionManager are linked.

I wouldn't be sure of that. From what you describe, either
A) they are not linked - configuration issue of Spring, on which I'm afraid I can't help you
B) you're not committing the transaction on Spring either

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: save(obj) does not commit the JDBC transaction
PostPosted: Thu Mar 31, 2011 6:53 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I have no idea what this "readonly" stands for in your Spring configuration, but I'd check that as well, to make sure Spring isn't rollbacking the TX.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: save(obj) does not commit the JDBC transaction
PostPosted: Thu Mar 31, 2011 7:37 am 
Newbie

Joined: Thu Mar 31, 2011 4:37 am
Posts: 11
s.grinovero wrote:
B) you're not committing the transaction on Spring either

I have no idea what this "readonly" stands for in your Spring configuration, but I'd check that as well, to make sure Spring isn't rollbacking the TX.

Well as soon as i do a getSession().disconnect() or as soon as i stop the application server, the database is updated - synchronized with hibernate's modification.
So i guess there is no problem with the transaction.

One more thing which is really weird, if I remove these properties in my hibernate.hbm.xml and use instead a direct connection to the oracle database, the same code works perfectly..

However, i'll try to take a look at the spring configuration..

Thanks a lot for your replies!


Top
 Profile  
 
 Post subject: Re: save(obj) does not commit the JDBC transaction
PostPosted: Fri Apr 01, 2011 10:22 am 
Newbie

Joined: Thu Mar 31, 2011 4:37 am
Posts: 11
Dear,

In my getSession() method , Instead of
Code:
Session sess = SessionFactoryUtils.getSession(sessionFactory, allowCreate);

I used the following code to get a session :
Code:
Session sess = sessionFactory.openSession();

As you know, in the first method, i had to force sess.connection().setAutoCommit(true);
With this second method, it works without the autocommit line, but instead, data aren't updated until i use sess.flush().

What's the difference between the two methods ? Isn't the SessionFactoryUtils necessary to manage the session properly?

I Hope you can help me to understand this.


Top
 Profile  
 
 Post subject: Re: save(obj) does not commit the JDBC transaction
PostPosted: Fri Apr 01, 2011 10:47 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
we didn't write "SessionFactoryUtils" so I've no clue about what your doing there, but it seems you found the error :)

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: save(obj) does not commit the JDBC transaction
PostPosted: Tue Apr 05, 2011 3:07 am 
Newbie

Joined: Thu Mar 31, 2011 4:37 am
Posts: 11
I'm still not mastering it but whatever, it works ^^
I'll mark it as resolved.

Thanks a lot for your answers !

Ps: Ohh, it seems like there is no "resolved" status available on the forum.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.