MySQL transactional, JDBC transaction operations:
See this article in detail: more details
Http://www.cnblogs.com/xuyiqing/p/8430214.html
How to configure isolation levels in Hibernate:
In the re-core configuration file:
<!-- Specifies the isolation level #hibernate when hibernate operates the database . connection.isolation 1|2|4|8 0001 1 READ UNCOMMITTED 0010 2 Read submitted 0100 4 Repeatable read 8 serialization - < name = "Hibernate.connection.isolation" > 4 </ Property >
Here is binary, converted to decimal is 1,2,4,8
Managing transactions in Projects:
Do not learn hibernate framework before, in project, open transaction in Business Layer (service), commit after execution or rollback
In the hibernate framework, as well, the operation of the database needs to use the session object, it is guaranteed that the service layer and the DAO layer of the session is the same
Similar to the servlet project, it is necessary to ensure that the connection objects of the service layer and the DAO layer are consistent, when binding threads are used
(This is a tool class in the previous Sevlet project, which can be viewed under comparison)
PackageUtils;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;ImportJavax.sql.DataSource;ImportCom.mchange.v2.c3p0.ComboPooledDataSource; Public classDatasourceutils {Private StaticDataSource DataSource =NewCombopooleddatasource (); Private Staticthreadlocal<connection> TL =NewThreadlocal<connection>(); //direct access to a connection pool Public StaticDataSource Getdatasource () {returnDataSource; } //Get Connection Object Public StaticConnection getconnection ()throwsSQLException {Connection con=Tl.get (); if(Con = =NULL) {con=datasource.getconnection (); Tl.set (con); } returncon; } //Open Transaction Public Static voidStartTransaction ()throwsSQLException {Connection con=getconnection (); if(Con! =NULL) {Con.setautocommit (false); } } //Transaction Rollback Public Static voidRollback ()throwsSQLException {Connection con=getconnection (); if(Con! =NULL) {con.rollback (); } } //commit and close resources and release from Threadlocall Public Static voidCommitandrelease ()throwsSQLException {Connection con=getconnection (); if(Con! =NULL) {con.commit ();//Transaction CommitCon.close ();//Close ResourceTl.remove ();//Remove from thread binding } } //Close Resource Method Public Static voidCloseConnection ()throwsSQLException {Connection con=getconnection (); if(Con! =NULL) {con.close (); } } Public Static voidClosestatement (Statement St)throwsSQLException {if(St! =NULL) {st.close (); } } Public Static voidCloseresultset (ResultSet rs)throwsSQLException {if(rs! =NULL) {rs.close (); } }}
View Code
In the hibernate framework, the principle is the same, the bound thread
However, you only need to configure it in the core configuration file:
<!--- <name = "Hibernate.current_ Session_context_class ">thread</Property>
The code needs only one line:
New Configuration (). Configure (). Buildsessionfactory (); Sf.getcurrentsession ();
A simple test can be understood:
Packagedemo;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration;Importorg.junit.Test;ImportUtils. Hibernateutils;//Test Getcurrentsession Public classDemo {@Test//returns the same session bound to the thread Public voidfun1 () {Session Session1=hibernateutils.getcurrentsession (); Session Session2=hibernateutils.getcurrentsession (); System.out.println (Session1==session2);//true} @Test//return to a different session Public voidfun2 () {Session Session1=hibernateutils.opensession (); Session Session2=hibernateutils.opensession (); System.out.println (Session1==session2);//false } }
Fill in the tool class:
PackageUtils;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.cfg.Configuration; Public classHibernateutils {Private Staticsessionfactory SF; Static{ //1 creating, calling NULL parameter constructsConfiguration conf =NewConfiguration (). Configure (); //2 Create a Sessionfactory object based on configuration informationSF =conf.buildsessionfactory (); } //Get session = Get a new session Public StaticSession opensession () {//3 Getting sessionSession session =sf.opensession (); returnsession; } //Get session = Get a session with thread binding Public StaticSession getcurrentsession () {//3 Getting sessionSession session =sf.getcurrentsession (); returnsession; } }
View Code
Attention:
Session object obtained through the Getcurrentsession method, the session is automatically closed when the transaction is submitted, do not close manually, otherwise there will be an exception
Hibernate Framework Learning Note 6: Transactions