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();
}
}

最新文章

  1. Android Touch事件分发机制学习
  2. swift上传头像
  3. 【leetcode❤python】66. Plus One
  4. 使用JDBC连接数据库
  5. windows7下,protel 99se元件库加载问题的解决方案
  6. navicat for mysql 显示中文乱码解决办法
  7. LINUX下FD_SET介绍
  8. Polipo
  9. Pangolin的使用
  10. 用HTML5实现的各种排序算法的动画比較
  11. C++ 中缀转后缀表达式并求值
  12. Spring Boot 框架的依赖管理
  13. 《笨方法学Python》加分题33
  14. python解析处理snmp回显----snmp
  15. shell反弹总结
  16. 关于QT_Creator不能在线调试问题
  17. GoogLeNetv1 论文研读笔记
  18. QThreadPool线程池的使用,线程与Widget通过信号与槽的方式通信。
  19. android开发(35) fragment和actionbar组合使用。解决不触发onOptionsItemSelected的问题,获得actionbar 的默认 get icon
  20. Netty学习第六节实例一步学习

热门文章

  1. PHP性能:序——谈性能
  2. LNMP环境搭建之php安装,wordpress博客搭建
  3. 【JMeter4.0学习(一)】之FTP性能测试脚本开发,并实现上传下载功能
  4. iOS系列译文:自定义Collection View布局
  5. github-readme.md-格式
  6. JavaScript-Confirm用法
  7. python基础-第五篇-5.1冒泡排序
  8. 我的Android进阶之旅------>解决 Error: ShouldNotReachHere() 问题
  9. java使用poi读写Excel
  10. Javaweb--- EL表达式 JSTL标准标签库