Atitit.spring Hibernate transaction mechanism spring cannot save object resolution

Source: Internet
Author: User

Atitit.spring hibernate transaction mechanism Spring cannot save object resolution

sessionfactory . Open Session ()

No.

Log Black horse SQL language :

sessionfactory . Getcurrentsession (). Update (user);

Middle Walk OK lan . Log blackheads are also tired .

Hibernateis used in Spring . Suppose we configured the transactionmanager. Then we should not call sessionfactory 's opensession () to get sessioin. Because this Session was not managed by the transaction.

Author:: Old Wow's paw attilax Ayron, email:[email protected]

Reprint please indicate source: Http://blog.csdn.net/attilax

the session created with getcurrentsession () is bound to the current thread and is created with opensession () . the session does not.

The session created with getcurrentsession () is in commit or rollback will be closed on its own initiative. the session created with opensession () must be closed manually.

Using getcurrentsession () requires adding the following configuration in the hibernate.cfg.xml file, for example:

* assume that you are using a local transaction (jdbc transaction)

<property name= "Hibernate.current_session_context_class" >thread</property>

* assume that you are using a global transaction (JTA transaction)

<property name= "Hibernate.current_session_context_class" >jta</property>

Assuming that the Hibernate4is used, the transaction must be configuredwith getcurrentsession () . Or you can't get to the session .

3 Hibernatetemplate.getsessionfactory (). Getcurrentsession ()

We useSpringand theHibernatecombined. The most common use of operational databases may behibernatetemplate,hibernatetemplateis integrated with a very much used method, unfortunately noCreateQuerymethod, perhaps we useHibernatewhen you like to useQuery, we may encapsulatehibernatetemplate.getsessionfactory (). Getcurrentsession ()method to getSession,SessionCreateQuery. This is a method, but you should get an exception"CreateQuery without an active transaction", sinceuse hibernatetemplate.getsessionfactory (). Getcurrentsession (), you are using hibernate the transaction management, and you expect Spring the managed transaction is hibernatetemplate , so you will be prompted not to open the transaction exception, workaround: 1) use hibernate transaction processing. It's like using hibernate alone , but that's probably not what you want. 2) use hibernatetemplate 's hibernatecallback callback:

Most applications that use Hibernate need some form of " context-sensitive " session. A specific session is always valid throughout a specific context.

However, for different types of applications, why is this"Context"the next definition is usually difficult. A different context to"Current"This concept defines a different scope. In the3.0before the version number. UseHibernateThe program is either self-writtenThreadLocalthe contextSession, or useHibernateutilthis auxiliary class, either using a third-party framework (for example,SpringorPico), they provide a proxy-based(proxy)or based on interceptors .(interception)the context-sensitiveSession.  

From3.0.1The version number begins,Hibernateadded asessionfactory.getcurrentsession ()method. Start. It assumes the useJTAtransactions,JTAThe transaction defines the currentSessionthe scope and context(scope and context). HibernateThe development team is convinced that there are several independentJTA TransactionManagerstable availability, whether or not deployed to aJavathe container. Most(if not all of the applications should be usedJTAtransaction Management.

Based on this, the use ofJTAthe context-sensitiveSessionto meet all your needs.  

the better is. Starting from 3.1 . the background implementation of sessionfactory.getcurrentsession () is pluggable. So. We introduced the new extension interface (org.hibernate.context.CurrentSessionContext) and the new configuration parameters (hibernate.current_ Session_context_class). So as to what is the scope and context of the " current session" (scope and contexts) The definition of the "connector" is unplugged.

Hibernate4 no Session found for current thread reason - Gate one - program Ape's work , Program Ape's Life (Java,python,delphi combat ). htm

Spring Consolidation hibernate4: Transaction management . htm

the difference between getcurrentsession and opensession () -loveyout 's Column - Blog Channel -CSDN.NET.htm

Atitit.spring Hibernate transaction mechanism spring cannot save object resolution

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.