session:hibernate操作数据库的对象,session对象表示数据库的一个连接。
获得session对象的方法(都来自SessionFactory):
- openSession
- getCurrentSession:此方法需在hibernate.cfg.xml文档中进行配置
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
public class SessionTest {
@Test
public void testOpenSession(){
//获得配置对象
Configuration config=new Configuration().configure();
//获得服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//获得session对象
Session session=sessionFactory.openSession();
if (session!=null) {
System.out.println("session创建成功!");
}else {
System.out.println("session创建失败!");
}
}
@Test
public void testGetCurrentSession(){
//获得配置对象
Configuration config=new Configuration().configure();
//获得服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//获得session对象
Session session=sessionFactory.getCurrentSession();
if (session!=null) {
System.out.println("session创建成功!");
}else {
System.out.println("session创建失败!");
}
//注:使用此方法获得session对象需在hibernate.cfg.xml配置文档中添加:<property name="hibernate.current_session_context_class">thread</property>(本地事务)
}
}
openSession与getCurrentSession的区别:
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
public class SessionTest {
@Test
public void testOpenSession(){
//获得配置对象
Configuration config=new Configuration().configure();
//获得服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//获得session对象
Session session1=sessionFactory.openSession();
Session session2=sessionFactory.openSession();
System.out.println(session1==session2); //false
/*
if (session!=null) {
System.out.println("session创建成功!");
}else {
System.out.println("session创建失败!");
}
*/
}
@Test
public void testGetCurrentSession(){
//获得配置对象
Configuration config=new Configuration().configure();
//获得服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//获得session对象
Session session1=sessionFactory.getCurrentSession();
Session session2=sessionFactory.getCurrentSession();
System.out.println(session1==session2); //true,两个引用指向同一个对象
/*
if (session!=null) {
System.out.println("session创建成功!");
}else {
System.out.println("session创建失败!");
}*/
//注:使用此方法获得session对象需在hibernate.cfg.xml配置文档中添加:<property name="hibernate.current_session_context_class">thread</property>(本地事务)
}
@Test
public void testSaveStudentsWithOpenSession(){ //使用openSession方式来保存记录
//获得配置对象
Configuration config=new Configuration().configure();
//获得服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//创建session对象
Session session1=sessionFactory.openSession();
//开启事务
Transaction transaction=session1.beginTransaction();
//生成一个学生对象
Students s=new Students(1,"张三","男",new Date(),"北京");
session1.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
//connection参数就是连接对象
System.out.println("connection hashCode:"+connection.hashCode()); //打印结果:connection hashCode:492889816
}
});
//保存
session1.save(s);
//session1.close(); //若未关闭则该session和其指向的连接对象都没有释放
transaction.commit(); //提交事务
Session session2=sessionFactory.openSession();
transaction=session2.beginTransaction();
s=new Students(2,"李四","男",new Date(),"上海");
session2.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.out.println("connection hashCode:"+connection.hashCode()); //打印结果:connection hashCode:1130724579
}
});
session2.save(s);
transaction.commit();
//两次打印connection对象的hashCode不同,说明两次会话对应的connection对象不同。那么不显式关闭session对象,连接对象也不会释放,可能造成数据库连接池溢出。
}
@Test
public void testSaveStudentsWithGetCurrentSession(){ //使用getCurrentSession方式来保存记录
//获得配置对象
Configuration config=new Configuration().configure();
//获得服务注册对象
ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
//获得sessionFactory对象
SessionFactory sessionFactory=config.buildSessionFactory(serviceRegistry);
//创建session对象
Session session1=sessionFactory.getCurrentSession();
//开启事务
Transaction transaction=session1.beginTransaction();
//生成一个学生对象
Students s=new Students(1,"张三","男",new Date(),"北京");
session1.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
//connection参数就是连接对象
System.out.println("connection hashCode:"+connection.hashCode()); //打印结果:connection hashCode:738595705
}
});
//保存
session1.save(s);
//session1.close();
transaction.commit(); //提交事务(此方法自动关闭session,自动释放资源)
Session session2=sessionFactory.getCurrentSession();
transaction=session2.beginTransaction();
s=new Students(2,"李四","男",new Date(),"上海");
session2.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.out.println("connection hashCode:"+connection.hashCode()); //打印结果:connection hashCode:738595705
}
});
session2.save(s);
transaction.commit();
//两次打印connection对象的hashCode相同(单例模式),说明两次会话对应的connection对象相同,是同一个。
}
}