Announcement Announcement Module
Collapse
No announcement yet.
Spring, Hibernate, MySQL Stand-Alone App Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring, Hibernate, MySQL Stand-Alone App

    Spring: 2.0.7
    Hibernate Core: 3.2.5.ga
    Hibernate Annotation: 3.3.0 GA

    I am using Spring, Hibernate, C3P0 and MySQL in a stand-alone application. The app runs on its own and not in an application server or a container of any sort.

    I keep getting the below error. I'd appreciate all pointers on what I may have done wrong. Thanks...

    The log below is triggered by the following code:

    sessionFactory.getCurrentSession();

    20:41:50,953 DEBUG SessionFactoryUtils:315 - Opening Hibernate Session
    20:41:51,015 DEBUG SessionImpl:220 - opened session at timestamp: 11929381109
    20:41:51,015 DEBUG SessionFactoryUtils:771 - Closing Hibernate Session
    20:41:51,015 ERROR GeoLocEditorService:51 - ***Exception***
    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at org.springframework.orm.hibernate3.AbstractSession FactoryBean$TransactionAwareInvocationHandler.invo ke(AbstractSessionFactoryBean.java:300)
    at $Proxy17.getCurrentSession(Unknown Source)


    ===
    My service method is annoated with @Transactional.

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
    <property name="configLocation" value="/hibernate.cfg.xml"/>
    <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- Hibernate Transaction Manager -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/>

  • #2
    Any ideas anyone? Thanks...

    Comment


    • #3
      Without looking at the code, stack trace or hibernate configuration - not really.

      Comment


      • #4
        Originally posted by dejanp View Post
        Without looking at the code, stack trace or hibernate configuration - not really.
        Well, there is a stack trace and configuration in the original post. You can see that Spring tried to open the session only to close it immediately

        The stack trace shows what the code does

        Comment


        • #5
          One line is not a stack trace, you can't see the flow from it. Configuration is only partial Spring config, not Hibernate config.

          Comment


          • #6
            Come on dude. If you have an inkling then out with it and be help instead of being a pain. You can clearly see in the original post the call() that produced the Spring stack trace...the rest is in application code. And yes, there is a@ @transactional notation in the right place. The same code/config works fine in a Tomcat environment.

            Here is the hibernate config.

            <property name="hibernate.cglib.use_reflection_optimizer">tr ue</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MyS QLDialect</property>

            Originally posted by dejanp View Post
            One line is not a stack trace, you can't see the flow from it. Configuration is only partial Spring config, not Hibernate config.

            Comment


            • #7
              Does the database driver you are using support transactions?

              Comment


              • #8
                Originally posted by jonnio View Post
                Does the database driver you are using support transactions?
                Yes, I use mySQL 5.x, InnoDB and their appropriate JDBC driver.

                I am scratching my head on the following debug from within Spring. You can see the it opened then closed the session immediate...why the close?

                20:41:50,953 DEBUG SessionFactoryUtils:315 - Opening Hibernate Session
                20:41:51,015 DEBUG SessionImpl:220 - opened session at timestamp: 11929381109
                20:41:51,015 DEBUG SessionFactoryUtils:771 - Closing Hibernate Session

                Comment


                • #9
                  that would be a problem with your hibernate configuration (or with your database configuration).
                  Without more information noone will be able to help you further than that.

                  Comment


                  • #10
                    If you post the full stacktrace, then it's possible to see if a transaction proxy was applied. Is it also possible to see the code you are running here?

                    Comment


                    • #11
                      Originally posted by karldmoore View Post
                      If you post the full stacktrace, then it's possible to see if a transaction proxy was applied. Is it also possible to see the code you are running here?
                      You got the full relevant stack trace in the original post , but here we go, the absolutely full deal...

                      This line triggers the stack trace below...thx...

                      sessionFactory.getCurrentSession();


                      16:45:30,843 INFO MyComponentEditorDao:24 - createMyComponent(aMyComponent)...
                      16:45:30,859 DEBUG SessionFactoryUtils:315 - Opening Hibernate Session
                      16:45:30,921 DEBUG SessionImpl:220 - opened session at timestamp: 11935287308
                      16:45:30,921 DEBUG SessionFactoryUtils:771 - Closing Hibernate Session
                      16:45:30,921 ERROR MyComponentEditorService:43 - ***Exception***
                      org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
                      at org.springframework.orm.hibernate3.AbstractSession FactoryBean$TransactionAwareInvocationHandler.invo ke(AbstractSessionFactoryBean.java:300)
                      at $Proxy17.getCurrentSession(Unknown Source)
                      at myco.dao.hibernate.HibernateDao.getSession(Hiberna teDao.java:28)
                      at myco.MyComponent.dao.MyComponentEditorDao.createMy Component(MyComponentEditorDao.java:26)
                      at myco.MyComponent.service.editor.MyComponentEditorS ervice.createMyComponent(MyComponentEditorService. java:38)
                      at myco.MyComponent.tools.LoadMyComponentApp.go(LoadM yComponentApp.java:79)
                      at myco.MyComponent.tools.LoadMyComponentApp.main(Loa dMyComponentApp.java:91)

                      Comment


                      • #12
                        The full stacktrace is useful, as you can clearly see there is no transactional proxy in there. So are you using beanFactory or applicationContext? Is it possible to see the code that you are trying to run? Can you wrap it in [code] [ /code] tags to make it readable.

                        Comment


                        • #13
                          Here we go...thx!

                          Code:
                                  ClassPathResource resource = new ClassPathResource("spring-context.xml");
                                  BeanFactory factory = new XmlBeanFactory(resource);
                                  editor = (MyComponentEditorService)factory.getBean("myComponentEditorService");        
                                  // Some code to build to request...and then
                                  editor.createMyComponent(req);
                              // MyComponentEditorService...
                          
                              @Transactional(rollbackFor=Exception.class)
                              public void createMyComponent(CreateReq aReq)
                                  throws MyException
                              {
                                   editorDao.create(aReq.getData());
                              }
                          
                                   // Dao...
                                   // This line gives the propblem...
                          public void create()
                              throws Exception
                          {
                                   sessionFactory.getCurrentSession()
                          }
                          Originally posted by karldmoore View Post
                          The full stacktrace is useful, as you can clearly see there is no transactional proxy in there. So are you using beanFactory or applicationContext? Is it possible to see the code that you are trying to run? Can you wrap it in [code] [ /code] tags to make it readable.

                          Comment


                          • #14
                            Try using an ApplicationContext instead of a BeanFactory.

                            Comment


                            • #15
                              Originally posted by karldmoore View Post
                              Try using an ApplicationContext instead of a BeanFactory.
                              I followed your recommendation and it worked like a charm.

                              Thanks all for helping and thanks all for being persistent!!! Great crew hanging around here!!!

                              Code:
                              ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext("spring-context.xml");

                              Comment

                              Working...
                              X