Hibernate.cfg.xml

<!-- 设置默认事务隔离级别 
读未提交 1
读已提交 2
可重复读 4
串行化(不可并发) 8
-->
<property name="connection.isolation">2</property>


@Test
public void test2() throws Exception {
Session session = sessionFactory.openSession();// 打开Session
Transaction tx = null;
try {
tx = session.beginTransaction();// 开始事务
// ------------------------------------------------------------

User user = (User) session.get(User.class, 2);// 持久化状态
System.out.println(user.getName());

session.refresh(user);// 刷新Session缓存中对象的状态,即重新select一下
System.out.println(user.getName());

// ------------------------------------------------------------
tx.commit();// 提交事务
} catch (Exception e) {
tx.rollback();// 回滚事务
throw e;
} finally {
session.close();// 释放资源
}
}