See Demo:
Package edu.test;
Import java.sql.Connection;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import Edu.po.Users;
Import Edu.utils.HibernateSessionFactory; public class Transactionisolationtest {static Sessionfactory sessionfactory = Hibernatesessionfactory.getsessionfacto
Ry ();
public static void UpdateUser (int id, String password) {Session session = Sessionfactory.opensession ();
Transaction Transaction = Session.begintransaction ();
try{Users user = (users) session.get (Users.class, id);
User.setpassword (password);
Transaction.commit ();
}catch (Exception e) {transaction.rollback ();
}finally{Session.close ();
}} public static void Testreadrepeat () {Session session1 = sessionfactory.opensession ();
Session Session2 = Sessionfactory.opensession ();
try{session1.connection (). Settransactionisolation (Connection.transaction_repeatable_read); Users user1 = (users) session1.get (Users.class, 6);
System.out.println ("Session1" password= "+user1.getpassword ()");
UpdateUser (6, "6");
Session2.connection (). Settransactionisolation (Connection.transaction_repeatable_read);
Users user2 = (users) session2.get (Users.class, 6);
System.out.println ("Session2" password= "+user2.getpassword ()");
}catch (Exception e) {e.printstacktrace ();
}finally{Session1.close ();
Session2.close ();
}} public static void Main (String args[]) {testreadrepeat ();
}
}
First, Session2.connection (). Settransactionisolation (Connection.transaction_repeatable_read); This line of code to make a breakpoint, and then debug, you can see the result:
Hibernate:select users0_.id as id0_0_, users0_.version as version0_0_, users0_.username as username0_0_, Users0_.passwor D as password0_0_ from Test.users users0_ where users0_.id=?
"Session1" password=8,version=10
Next, execute SQL on the database client and modify the password=9:
Update users Set Password = 9 where id=6;
Finally, just debug the code, let it run down, but found that the results and the first get () get the same object .... :
Hibernate:select users0_.id as id0_0_, users0_.version as version0_0_, users0_.username as username0_0_, Users0_.passwor D as password0_0_ from Test.users users0_ where users0_.id=?
"Session1" password=8,version=10
hibernate:select users0_.id as id0_0_, users0_.version as version0_0_, users0_. Username as username0_0_, Users0_.password as password0_0_ from Test.users users0_ where users0_.id=?
"Session2" password=8,version=10
Later, I thought about, in two times get () Get the Users table information between a modified password database operation, that is, the comment code//updateuser (6, "6");
Release the annotated UpdateUser code, then run the program, found two times get () get the object is not the same .... :
Hibernate:select users0_.id as id0_0_, users0_.version as version0_0_, users0_.username as username0_0_, Users0_.passwor D as password0_0_ from Test.users users0_ where users0_.id=?
"Session1" password=9,version=10
hibernate:select users0_.id as id0_0_, users0_.version as version0_0_, users0_. Username as username0_0_, Users0_.password as password0_0_ from Test.users users0_ where users0_.id=?
Hibernate:update test.users set version=?, Username=?, password=? where id=? and version=?
Hibernate:select users0_.id as id0_0_, users0_.version as version0_0_, users0_.username as username0_0_, Users0_.passwor D as password0_0_ from Test.users users0_ where users0_.id=?
"Session2" password=6,version=11