Announcement Announcement Module
Collapse
No announcement yet.
AnnotationTransactionAspect and duplicate HibernateSessions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AnnotationTransactionAspect and duplicate HibernateSessions

    Hello!

    I use AspectJ to make my methods transaction-aware.

    But the problem I have is, that Spring-HibernateTemplate called inside
    a transaction doesn´t use the same Hibernate-Session as the Transaction did.

    When I use Spring-AOP-Transactions instead, it works.

    Here is my simplified example. In this example I have a read method so it does not matter that much. But when I have write methods the two sessions
    cause a deadlock.

    DAO:
    Code:
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
    public class PollDao extends HibernateDaoSupport implements PollDaoI {
    ...
    @SuppressWarnings("unchecked")
    public List<Poll> getAllPolls() {
    	return getHibernateTemplate().find("from Poll");
    }
    beans.xml:
    Code:
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <bean
    class="org.springframework.transaction.aspectj.AnnotationTransactionAspect"
    factory-method="aspectOf">
    <property name="transactionManager" ref="transactionManager" />
    </bean>
    
    ... DAOs, HibernateTemplate, SessionFactory, DataSource
    here´s the debug-output. To make things short, the last three lines show that two hibernate-sessions are open. But there should be open only one.

    02/23 23:08:24 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Using transaction object [org.springframework.orm.hibernate3.HibernateTransa ctionManager$HibernateTransactionObject@1c60f74]
    02/23 23:08:24 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Creating new transaction with name [domain.repositories.PollDao.getAllPolls]: PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE
    02/23 23:08:33 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Opened new Session [org.hibernate.impl.SessionImpl@1f5e4b6] for Hibernate transaction
    02/23 23:08:34 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@1f5e4b6]
    02/23 23:08:37 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Exposing Hibernate transaction as JDBC transaction [com.mchange.v2.c3p0.impl.NewProxyConnection@12c734 c]
    02/23 23:08:52 DEBUG org.springframework.orm.hibernate3.SessionFactoryU tils - Opening Hibernate Session
    02/23 23:08:55 DEBUG org.springframework.orm.hibernate3.SessionFactoryU tils - Registering Spring transaction synchronization for new Hibernate Session
    02/23 23:08:56 DEBUG org.springframework.orm.hibernate3.HibernateTempla te - Found thread-bound Session for HibernateTemplate
    Hibernate: select poll0_.id as id0_, poll0_1_.version as version0_, poll0_.description as descript2_1_, poll0_.name as name1_, poll0_.position as position1_, poll0_.state as state1_ from Poll poll0_ inner join Entity poll0_1_ on poll0_.id=poll0_1_.id
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.HibernateTempla te - Not closing pre-bound Hibernate Session after HibernateTemplate
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Triggering beforeCommit synchronization
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.SessionFactoryU tils - Flushing Hibernate Session on transaction synchronization
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Triggering beforeCompletion synchronization
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Initiating transaction commit
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1f5e4b6]
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Triggering afterCommit synchronization
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Triggering afterCompletion synchronization
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.SessionFactoryU tils - Closing Hibernate Session
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.HibernateTransa ctionManager - Closing Hibernate Session [org.hibernate.impl.SessionImpl@1f5e4b6] after transaction
    02/23 23:09:00 DEBUG org.springframework.orm.hibernate3.SessionFactoryU tils - Closing Hibernate Session

    I have looked for what´s going inside. Essentially TransactionManager and
    HibernateTemplate both lookup the Session in TransactionSynchronizationManager. This object holds ThreadLocal variable to store the Hibernate sessions. So it should work for both AOP-Transactions
    and AspectJ-Transactions, I think. So what´s wrong?
    Remark: I use Spring 2.0.4

    Thank you very much in advance

    Jörg

  • #2
    this look real serious

    Comment

    Working...
    X