用ThreadLocal来管理Connection和Transaction

1 /*
2 * Created on 2003-11-16
3 *
4 */
5 package com.javaeye.crm;
6
7 import net.sf.hibernate.HibernateException;
8 import net.sf.hibernate.Session;
9 import net.sf.hibernate.SessionFactory;
10 import net.sf.hibernate.Transaction;
11 import net.sf.hibernate.cfg.Configuration;
12 import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
13
14 /**
15 *
16 * 获取Session连接工厂类
17 *
18 * @author Robbin Fan
19 *
20 */
21 public class HibernateSession {
22
23 private static final ThreadLocal sessionThread = new ThreadLocal();
24
25 private static final ThreadLocal transactionThread = new ThreadLocal();
26
27 private static SessionFactory sf = null;
28
29 /**
30 * 获取当前线程使用的Session
31 *
32 * @return Session
33 * @throws HibernateException
34 */
35 public static Session currentSession() throws HibernateException {
36 Session s = (Session) sessionThread.get();
37 if (s == null) {
...}
38 if (sf == null) sf = new Configuration().configure().buildSessionFactory();
39 s = sf.openSession();
40 sessionThread.set(s);
41 }
42 return s;
43 }
44
45 /**
46 * 获取一个新的Session
47 *
48 * @return Session
49 * @throws HibernateException
50 */
51 public static Session newSession() throws HibernateException {
52
53 if (sf == null) sf = new Configuration().configure().buildSessionFactory();
54 Session s = sf.openSession();
55 return s;
56 }
57
58 /**
59 * 启动或者加入当前Session的Transaction
60 *
61 * @return Transaction
62 * @throws HibernateException
63 */
64 public static Transaction currentTransaction() throws HibernateException {
...}
65 Transaction tx = (Transaction) transactionThread.get();
66 if (tx == null) {
...}
67 tx = currentSession().beginTransaction();
68 transactionThread.set(tx);
69 }
70 return tx;
71 }
72
73 /**
74 * 提交当前Session的Transaction
75 *
76 * @throws HibernateException
77 */
78 public static void commitTransaction() throws HibernateException {
...}
79 Transaction tx = (Transaction) transactionThread.get();
80 transactionThread.set(null);
81 if (tx != null) tx.commit();
82 }
83
84 /**
85 * 回滚当前事务
86 *
87 * @throws HibernateException
88 */
89 public static void rollbackTransaction() throws HibernateException {
...}
90 Transaction tx = (Transaction) transactionThread.get();
91 transactionThread.set(null);
92 if (tx != null) tx.rollback();
93 }
94
95 /**
96 * 关闭当前线程使用的Session
97 *
98 * @throws HibernateException
99 */
100 public static void closeSession() throws HibernateException {
101 Session s = (Session) sessionThread.get();
102 sessionThread.set(null);
103 if (s != null) s.close();
104 }
105
106 /**
107 * 根据映射文件和持久对象生成数据库DDL,生成文件为create_table.sql
108 *
109 * @param args 参数
110 */
111 public static void main(String[] args) {
...}
112 try {
...}
113 String conf = "hibernate.cfg.xml";
114 if (args.length != 0) conf = args[0];
115 Configuration cfg = new Configuration().configure("/" + conf);
116 SchemaExport se = new SchemaExport(cfg);
117 //se.setOutputFile("create_table.sql");
118 se.create(true,true);
119 } catch (HibernateException e) {
120 System.out.println(e.getMessage());
121 }
122
123 }
124 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值