1 本章目的:获得session的两种方式:
openSession 和 getCurrentSession
2 两种session的使用方法
1openSession可以直接写,getCurrentSession需要在cfg.xml中配置
2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭
这里getCurrentSession本地事务(本地事务:jdbc)时 要在配置文件里进行如下设置
* 如果使用的是本地事务(jdbc事务) <property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务) <property name="hibernate.current_session_context_class">jta</property>
3 两种session用途不同之出
getCurrentSession创建的session会和绑定到当前线程,而openSession不会。
demo:
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/bendi</property>
<property name="connection.username">root</property>
<property name="connection.password">weidou318</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource = "Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
StudentTest.java
package hibernate_001;
import java.sql.Array; import java.sql.Blob; import java.sql.CallableStatement; import java.sql.Clob; import java.sql.DatabaseMetaData; import java.sql.NClob; import java.sql.PreparedStatement; import java.sql.SQLClientInfoException; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.SQLXML; import java.sql.Savepoint; import java.sql.Statement; import java.sql.Struct; import java.util.Date; import java.util.Map; import java.util.Properties; import java.util.TimeZone; import java.util.concurrent.Executor;
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.metamodel.SessionFactoryBuilder; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test;
import com.ddwei.student.Student; import com.mysql.jdbc.Connection; import com.mysql.jdbc.ExceptionInterceptor; import com.mysql.jdbc.Extension; import com.mysql.jdbc.MySQLConnection; import com.mysql.jdbc.log.Log;
public class StudentTest { private SessionFactory sessionFactory; private Session session; private Transaction trasaction; @Test public void testSaveStudent1(){ Configuration config = new Configuration().configure(); ServiceRegistry serviceRe = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRe); Session session = sessionFactory.openSession(); // Student student =new Student(3,"周恩来","男",new Date(),"绍兴");//创建学生对象 // session.save(student);//会话保存学生对象进入数据库 } @Test public void testSaveStudent2(){ Configuration config = new Configuration().configure(); ServiceRegistry serviceRe = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory = config.buildSessionFactory(serviceRe); Session session = sessionFactory.getCurrentSession(); // Student student =new Student(3,"周恩来","男",new Date(),"绍兴");//创建学生对象 // session.save(student);//会话保存学生对象进入数据库 }
}