Programming exception-Suppose you quote createsqlquery is not valid without active transaction,...

Source: Internet
Author: User

Very often when we use Hibernate's session, we keep the session unique in an execution environment.

For example, in the same line range with the same session. Use the same session within the same method so that we can use the cached data in the session. But. What I want to say is not the cache, and listen to my one by one-way.
Recent trial spring3.0.2+struts2.18+hibernate3.3.2 Learning to build a Web project, there has been a rather depressing problem.

Is that I've configured spring to manage hibernate transactions when I run Hibernate in DAO. such as Save. Delete,update,createquery, always saying that you cannot run in an inactive transaction (Org.hibernate.HibernateException:createSQLQuery is not valid without Active transaction). Google check now. Nothing. There were several doubts about whether the configuration was written out. DAO or service is wrong, the change to change is still problematic. At that time quite depressed ah, think Ah, you spring is not to help me manage affairs? You do not open your own business, but also I have to manually open AH. Check the spring documentation immediately. From Chinese to English. There is no clue to the value of the test, it is quite a blow. Code to change one pass. It is normal to find that data is manipulated using spring's hibernatetemplate. Do not forget to check the Hibernate doc, an inattentive to brother found a lengthy configuration properties: Hibernate.current_session_context_class. Your heart is so great that you are the one. Sample of. Brother put you out.
what does Hibernate.current_session_context_class do with it? Popular point of speaking. is to configure the session to bind to an execution environment, such as a session that is obtained with getcurrentsession () from the same thread. Currently there is no session on their own initiative to create a return to your use. The problem is here. Official documentation such as the following: most applications that use Hibernate need some form of "context-sensitive" session. Specific sessions throughout the special
is always valid within the context range. However, it is generally difficult for different types of applications to make the next definition of this "context". Different contexts define different scopes for the concept of "current". Before the 3.0 version number, programs that use Hibernate either adopt a self-written ThreadLocal-based context session. Either using Hibernateutil as an auxiliary class or using a third-party framework (such as Spring or Pico), they provide a context-sensitive session based on proxy or interceptor (interception).

Hibernate added the Sessionfactory.getcurrentsession () method starting with the 3.0.1 Version number.

At the beginning, it assumes the use of JTA transactions. The JTA transaction defines the scope and context of the current session (scopes and contexts). Because of the stable availability of several independent JTA TransactionManager, most (if not all) applications should use JTA transaction management, regardless of whether they are deployed in a Java EE container. Based on this point. A context-sensitive session with JTA can satisfy all your needs.



To see my configuration again, the value of Hibernate.current_session_context_class is set to thread.

As I have simply understood, the session returned by Getcurrentsession () is bound to the current thread of execution. It is more professional to say that the context of this session is thread, but not the session object that spring has hosted. Again with brother that thigh thought a few, instantly learned some. So the session obtained is in the current thread outside the context of the spring proxy. So this session is not the session of the transaction manager agent and does not open the transaction on its own initiative. According to the official tip: The third-party framework provides agent-based (proxy) or interceptor (interception)-based context-sensitive session management, so the Hibernate.current_session_context_class settings are removed. Everything went back to the calm days.
Referenceshttp://justsee.iteye.com/blog/1061576, and finally understand the causes and consequences of the problem. Extracts such as the following:

The sessionfactory configuration file in Ssh2 should be hibernate.current_session_context_ Class is set to Org.springframework.orm.hibernate3.SpringSessionContext (this value is implied). and apply spring management transactions.
assuming that <prop key= "Hibernate.current_session_context_class" >thread</prop> will report an exception,
The reason is still the hibernate.current_session_context_class problem in spring
Localsessionfactorybean The source code in the Spring class. Method Buildsessionfactory will Hibernate.current_session_context_ Class set to Org.springframework.orm.hibernate3.SpringSessionContext


Wisdom Podcast Chengdu Java Training Center: Http://cd.itcast.cn?140831ls

Programming exception-Suppose you quote createsqlquery is not valid without active transaction,...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.