Atitit. spring hibernate transaction mechanism spring cannot save the solution of objects, springhibernate transactions

Source: Internet
Author: User

Atitit. spring hibernate transaction mechanism spring cannot save the solution of objects, springhibernate transactions

Atitit. spring hibernate transaction mechanism spring cannot save the solution of Objects

 

SessionFactory. openSession ()

No .. Log blackhead Trojan SQL language ..

 

 

 

SessionFactory. getCurrentSession (). update (user );

In the middle, go to OK lan.. log black head is also tired ..

 

 

Use Hibernate in Spring. If TransactionManager is configured, we should not call the openSession () of SessionFactory to obtain Sessioin, because the obtained Session is not managed by transactions.

 

Author: old wow's paw Attilax iron, EMAIL: 1466519819@qq.com

Reprinted please indicate Source: http://blog.csdn.net/attilax

 

 

Sessions created using getCurrentSession () are bound to the current thread, but sessions created using openSession () are not.

Sessions created using getCurrentSession () are automatically closed at commit or rollback, while those created using openSession () must be closed manually.

To use getCurrentSession (), add the following configuration to the hibernate. cfg. xml file:

* If you are using a local transaction (jdbc transaction)

<Property name = "hibernate. current_session_context_class"> thread </property>

* If you are using a global transaction (jta transaction)

<Property name = "hibernate. current_session_context_class"> jta </property>

If Hibernate4 is used, the transaction must be configured when getCurrentSession () is used; otherwise, the session cannot be obtained.

 

 

 

3 hibernateTemplate. getSessionFactory (). getCurrentSession ()

We use spring and hibernate in combination. The most common database operations may be HibernateTemplate. HibernateTemplate integrates many methods of use. Unfortunately, there is no createQuery method, maybe we like Query when using hibernate, and we may encapsulate hibernateTemplate. getSessionFactory (). the getCurrentSession () method gets the Session, and the session creates the Query. This is a method, but you should get the exception "createQuery without an active transaction" because hibernateTemplate is used. getSessionFactory (). getCurrentSession (), you are using hibernate's transaction management, and you expect spring to manage the transaction as hibernateTemplate, so you will The system prompts that the transaction has not been opened. Solution: 1) Use hibernate for transaction processing, just like using hibernate alone, but this may not be what you want. 2) use the HibernateCallBack callback of hibernateTemplate:

 

 

 

Most applications using Hibernate need some form of "Context-related" session. A specific session is always valid throughout a specific context. However, for different types of applications, it is usually difficult to make up the next definition of this "context". Different contexts define different scopes for the concept of "current. Prior to version 3.0, Hibernate programs were either self-compiled ThreadLocal-based context sessions, or auxiliary classes such as HibernateUtil, or third-party frameworks such as Spring or Pico ), they provide context-related sessions based on proxies or interception.

Starting from version 3.0.1, Hibernate added the SessionFactory. getCurrentSession () method. At the beginning, it assumes that a JTA transaction is used. a jta transaction defines the scope and context of the current session ). The Hibernate Development Team firmly believes that there are several independent JTA TransactionManager for stable availability, whether or not they are deployed in a J2EE container, the majority (if not all) all applications should adopt JTA transaction management. Based on this, JTA-based context-related sessions can meet all your needs.

The better thing is that, starting from 3.1, the background Implementation of SessionFactory. getCurrentSession () is pluggable. Therefore, we have introduced a new extended interface (org. hibernate. context. currentSessionContext) and new configuration parameters (hibernate. current_session_context_class) to plug in the scope and context definitions of the current session.

 

 

 

 

Hibernate4 No Session found for current thread reason-Number 1-programmer's work, programmer's life (Java, python,delphi .htm)

Spring integrates hibernate4: Transaction Management .htm

Difference between getCurrentSession and openSession ()-LoveYouT column-blog channel-CSDN.NET.htm

 

Related Article

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.