Hibernate学习---第十五节:hibernate之session线程安全
来源:互联网 发布:三枪内裤怎么样 知乎 编辑:程序博客网 时间:2024/05/21 05:19
1、hibernate.cfg.xml 文件中添加如下代码开启线程安全:
<property name="hibernate.current_session_context_class">thread</property>
具体如下:
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.current_session_context_class">thread</property> <mapping resource="learn\hibernate\bean\Person.hbm.xml"/> </session-factory></hibernate-configuration>
2、测试:
package learn.hibernate.test;import static org.junit.Assert.*;import learn.hibernate.bean.Person;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;public class TestHibernate { SessionFactory factory = null; Session session = null; Transaction tx = null; /** * 测试之前初始化数据 * @throws Exception */ @SuppressWarnings("deprecation") @Before public void setUp() throws Exception { System.out.println("---------初始化数据----------"); Configuration config = new Configuration().configure(); ServiceRegistry sr = new ServiceRegistryBuilder() .applySettings(config.getProperties()).buildServiceRegistry(); factory = config.buildSessionFactory(sr); //session = factory.openSession(); // 获得一个线程安全的session,多线程环境下使用 session = factory.getCurrentSession(); } /** * 测试之后释放(销毁)数据 * @throws Exception */ @After public void tearDown() throws Exception { System.out.println("---------释放数据----------"); // 获得的是线程安全的session,不需要程序控制关闭,事务提交后就会自动关闭 /*if(session.isOpen()){ session.close(); }*/ } /** * 批量写入数据 */ @Test public void testAdd(){ tx = session.beginTransaction(); Person person = new Person("admin", 22, 123456, null); session.persist(person); tx.commit(); } @Test public void testGet(){ // 如果获得的是线程安全的session,那么要开启事务 tx = session.beginTransaction(); Person p = (Person)session.get(Person.class, 12); System.out.println(p); tx.commit(); }}
阅读全文
0 0
- Hibernate学习---第十五节:hibernate之session线程安全
- hibernate学习之 Session
- Hibernate之Session线程本地化
- hibernate配置文件hibernate.cfg.xml安全线程的session配置
- Hibernate学习---第五节:hibernate二级缓存
- Hibernate第十一篇【配置C3P0数据库连接池、线程Session】
- hibernate之传播Hibernate Session(通过线程局部传播)
- Hibernate中使用Threadlocal创建线程安全的Session
- Hibernate中使用Threadlocal创建线程安全的Session
- Hibernate中使用Threadlocal创建线程安全的Session
- 线程安全hibernate的session管理类实现
- php学习 第十五节
- hibernate学习之 session的产生方式
- Hibernate学习笔记之Session管理
- 7.7--SSH学习之Hibernate Session
- Hibernate学习笔记 之 configuration、sessionFactory、session
- Spring学习之(Hibernate)Session接口
- Hibernate 之 session
- Hibernate学习---第十一节:Hibernate之QBC、样例查询&离线查询
- 冒泡排序法相关解析
- Hibernate学习---第十二节:Hibernate之数据抓取策略&批量抓取
- Hibernate学习---第十三节:Hibernate之锁机制&乐观锁实现
- Hibernate学习---第十四节:hibernate过滤器和拦截器的实现
- Hibernate学习---第十五节:hibernate之session线程安全
- Java设计模式之装饰者模式
- 微服务、分布式、公有云、私有云
- Spring介绍
- 数据流图和数据流程图的区别
- 为啥看到大神级的程序员基本都是外国人的形象
- Win10操作系统下,Java环境变量的配置和Java入门小程序“hello,world”的编译
- Vue.js 计算属性
- MyBatis入门基础(一) 为什么要用Mybatis