3

I've 'inherited' a project which uses Spring annotations to manage transactions/sessions with Hibernate. Or at least it's meant to be. Currently the Hibernate sessions never get flushed (they are set to FLUSH_MODE_NEVER) and the DAO's need to be flushed by hand for any data to be written to the database.

Also all the DTO objects stay resident in hibernate's memory, eventually leading to an OutOfMemory error.

I believe I need to tell Spring/Hibernate to close the session or commit the transaction. In my controller class I have the annotated method for handling the requests:

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    ...
    ...
}

and in the applicationContetxt.xml file I believe I setup the hibernate transaction manager and tell spring to use the annotations:

<!-- hibernate3 transaction manager -->
<bean id="transactionManagerLocal" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="${local.data.source}" />
    <property name="sessionFactory" ref="localSessionFactory" />
</bean>

<!-- Demarcate using @Transactional annotation -->
<tx:annotation-driven transaction-manager="transactionManagerLocal" order="200" />

Not only am I pretty sure that the config is wrong by the way data doesn't get written to the DB without manually calling flush on each of the DAO's, from the log file we can see that transaction manager has it's flushing and session closing disabled:

INFO  [http-8080-2] TransactionFactoryFactory - Transaction strategy: org.springframework.orm.hibernate3.SpringTransactionFactory
INFO  [http-8080-2] TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
INFO  [http-8080-2] SettingsFactory - Automatic flush during beforeCompletion(): disabled
INFO  [http-8080-2] SettingsFactory - Automatic session close at end of transaction: disabled

What needs to be done to make Spring/hibernate automatically flush the DAOs and/or close the session to prevent huge amounts of memory being used by Hibernate?

cheers Dan

<!-- MySQL/InnoDB session factory -->
<bean id="localSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="${local.data.source}"/>
    <property name="mappingResources">
        <list>
            <value>net/company/projectname/domain/ExchangeRate.hbm.xml</value>
            <!-- Other  -->

        </list>
    </property>

   <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
            <prop key="hibernate.connection.zeroDateTimeBehavior">convertToNull</prop>
            <!-- 
            DOES NOTHING
            <prop key="hibernate.transaction.flush_before_completion">true</prop>
            <prop key="hibernate.transaction.auto_close_session">true</prop>
            -->
        </props>
    </property>
</bean>

<!-- hibernate3 transaction manager -->
<bean id="transactionManagerLocal" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="dataSource" ref="${local.data.source}" />
    <property name="sessionFactory" ref="localSessionFactory" />
</bean>


<!-- Demarcate using @Transactional annotation -->
<tx:annotation-driven transaction-manager="transactionManagerLocal" order="200" />

<bean id="exchangeRateDAO" class="net.company.project.dao.hibernate.impl.ExchangeRateDAOImpl">
     <property name="sessionFactory" ref="localSessionFactory"/>
 </bean>     
1
  • btw I've tried adding <prop key="hibernate.transaction.flush_before_completion">true</prop> <prop key="hibernate.transaction.auto_close_session">true</prop> to the configuration files. Although it now says that automatic flush and session close are enabled, still no data gets written to the DB without manually flushing the DAOs.
    – Danack
    Jan 31, 2012 at 0:26

4 Answers 4

6

One of my cleverer colleagues discovered what the problem was.

The actual problem was that the method that you've declared as @Transactional is an inherited method that is called from a base class, which means that Spring is unable to intercept calls to the method and wrap it in a transaction.

Spring implements transaction management as aspects, and aspects are implemented with proxies. The limitation of this is that if an object calls a method on itself (which is what's happening here because of inheritance) then the proxy doesn't see the call (because it happens internally within the class, like calling a private method), and can't so anything about it.

Which makes sense but seems to be incredibly dangerous as it fails to write any data without any error message or warning.

4
  • How did you solve it finally? By removing the @Transactional annotation? Jan 31, 2013 at 7:37
  • I moved the method that had the @Transactional annotation around it to a different class. Because it was now in a different class, the way that Spring proxies any class also worked. When it was in the same class as the code that was calling it, the proxy class was ignored as they don't work on internally called methods. Er, if that makes no sense I may need to draw a diagram to explain.
    – Danack
    Jan 31, 2013 at 13:14
  • is this the case also with interfaces or does it only concern inherited methods from superclases?
    – haki
    Jun 23, 2017 at 12:37
  • @haki it should only be when the method is being called on 'this'.
    – Danack
    Jun 23, 2017 at 15:01
2

there is another way instead of hibernate template( hibernate template will couple spring to hibernate that is why it is deprecated in Spring 3.1 ) the key to solve this issue is to remove any configuration for the hibernate except the dialct and the url in simple way,Remove the transactional stuff from your hibernate properties as You want spring to manage transactions, not hibernate. as Marten Deinum mentioned in this url http://forum.springsource.org/archive/index.php/t-47667.html here is a snippet of my spring xml configuration file

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">    
        <value>
            classpath:/com/spring/hibernate.cfg.xml
        </value>
    </property>       
    </bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

and here is the hibernate file

<session-factory> 
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<mapping resource="com/spring/BloodType.hbm.xml" />
</session-factory>

do not forget to write @Transactional in your method or class also use

Session session = sessionFactory.getCurrentSession();

and this

BloodType b = new BloodType();
b.setId(new Long(1));
BloodType b1 = (BloodType)session.get(BloodType.class, 12L);
session.delete(b1);
session.save(b);

would work just fine without any need for session.flush() nor transaction.commit() hope it works

1
  • Hi Ali, The problem was that Spring was not picking up the @Transactional annotation, due to the function that had it defined was being called from the same class, so Spring was not able to proxy the function call.
    – Danack
    Nov 22, 2012 at 6:38
1

using hibernate template solves it

@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
       hibernateTemplate = new HibernateTemplate(sessionFactory);
}

and then use the template like in this link http://singgihpraditya.wordpress.com/2010/02/13/spring-3-0-and-hibernate-tutorial-part-1/

your reply would be great to share

2
  • Hi Ali, unfortunately no longer have access to the code to test, but I don't believe your suggestion actually solves the problem.
    – Danack
    Nov 21, 2012 at 0:29
  • Hi Ali, unfortunately no longer have access to the code to test, but I don't believe your suggestion actually solves the actual problem. I needed the transactions to work where they were defined (i.e. around the handle request function). Moving the transactions to within the DAO would completely change the rollback behaviour.
    – Danack
    Nov 21, 2012 at 0:36
0

if the transaction (which is annotated) succeeds then transaction.commit() is called automatically which will write the contents to the DB, please check if the transaction is being called and there are no exceptions being thrown.

also ensure that you link your sessionfactory to the transcation which is declared.

7
  • There are no exceptions, but I cannot see anything in the logs about the transaction being committed. The only thing about sessions I can see is: INFO [http-8080-1] SessionFactoryImpl - building session factory INFO [http-8080-1] SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
    – Danack
    Jan 31, 2012 at 23:43
  • try running the app in debug mode to see if you get any commiting transaction entry. Feb 1, 2012 at 6:58
  • I get nothing...Adding logger.info("Is transaction active " + TransactionSynchronizationManager.isActualTransactionActive()); to the start of the request method shows that there is no transaction active.
    – Danack
    Feb 2, 2012 at 0:56
  • this means that the transaction AOP advice has not started.. try configuring things using xml (this is something i'm good at) ` <aop:config> <aop:pointcut id="pointCut" expression="execution(* com..servicehandlers..*(..))" /> <aop:advisor pointcut-ref="pointCut" advice-ref="txAdvice" /><!-- txAdvice is the transaction advice --> </aop:config>` Feb 2, 2012 at 4:06
  • Done, that - doesn't seem to be doing anything. DEBUG [http-8080-6] NameMatchTransactionAttributeSource - Adding transactional method [*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
    – Danack
    Feb 2, 2012 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.