Hibernate's Connection.transaction_repeatable_read misunderstanding

Source: Internet
Author: User

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


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.