Announcement Announcement Module
Collapse
No announcement yet.
trouble with <tx:annotation-driven/> and hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • trouble with <tx:annotation-driven/> and hibernate

    I'm new to Spring, but i've read a lot of documentation(even searched this forum for this topic) , but still can't figure this one out. need help from anyone with experience with @Transactional using <tx:annotation-driven>

    I have this:

    ...
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven/>

    ...


    "sessionFactory" is correctly defined.
    Then my service class (Which uses Hibernate 3.1) has this:

    ...

    @Transactional(propagation=Propagation.REQUIRES_NE W, readOnly=true)
    public User getUserByUsername(String username) {
    return ((User) getSession()
    .createQuery("from User u where u.username like :un")
    .setString("un", username)
    .setMaxResults(1)
    .uniqueResult());
    }

    ...

    then this is from the stack trace:

    ...

    09:51:11,491 ERROR AutoHandler:30 - createQuery is not valid without active transaction
    org.hibernate.HibernateException: createQuery is not valid without active transaction
    at org.hibernate.context.ThreadLocalSessionContext$Tr ansactionProtectionWrapper.invoke(ThreadLocalSessi onContext.java:297)
    at $Proxy39.createQuery(Unknown Source)
    at net.ehosp.model.daoImpl.UserDAOHibernate.getUserBy Username(UserDAOHibernate.java:61)
    at net.ehosp.controller.LoginActionBean.login(LoginAc tionBean.java:136)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)

    Any ideas? is this a problem with hibernate?

  • #2
    Your DAO instance is not being managed by Spring, and therefore the transactional proxy is not being applied. You should be getting the DAO instance from Spring (typically using dependency-injection into the calling object). How are you currently getting this object?

    Comment


    • #3
      Thanks. The DAO is being managed by Spring through dependency injection.

      Comment


      • #4
        How are you instantiating the spring context? A common mistake is to use of the BeanFactory implementations instead of a full-fledged ApplicationContext.

        Comment


        • #5
          I'm having the same problem. Have you found a solution?

          Comment


          • #6
            No I still haven't figured it out. I actually gave up for a while!

            In response to Plethora's question about how i'm instantiating Spring, well I'm using Stripes, so Spring is loaded by a Stripes interceptor (net.sourceforge.stripes.integration.spring.Spring Interceptor), which is configure in web.xml.

            I have a feeling it might have something to do with this, but i have no clue how. I'll post the question in the Stripes forum

            Comment


            • #7
              I found out my issue. I'm using the 2.1-m4 release. It appears to be a bug. I reverted to 2.0.7 and everything worked.

              Comment


              • #8
                please, what was your issue exactly, so i can see how to fix mine.

                Comment


                • #9
                  That's almost all there was to it. I'm using maven. I switched my dependency from 2.1-m4 to 2.0.7 and spring-test (from the new version) back to spring-mock. Then everything worked.

                  My setup was a DAO, xml configured sessionFactory, txManager, etc, and a junit test class extending AbstractTransactionalDataSourceSpringContextTests. Works with 2.0.7, not with 2.1-m4.

                  If you are using the new annotation stuff from 2.1 - the reason I wanted to give it a try - you may have to wait for an official release.

                  Comment

                  Working...
                  X