提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.dao.common; |
A |
2 |
|
|
3 |
import org.apache.log4j.Logger; |
|
4 |
import org.hibernate.Session; |
|
5 |
import org.hibernate.SessionFactory; |
|
6 |
import org.hibernate.Transaction; |
|
7 |
|
|
8 |
/** |
|
9 |
* @author mengly |
|
10 |
* @version 创建时间:2016年7月4日 上午11:23:28 类说明 |
|
11 |
* |
|
12 |
*/ |
|
13 |
|
|
14 |
public class TransactionContext { |
|
15 |
private Logger log = Logger.getLogger(TransactionContext.class); |
|
16 |
|
|
17 |
public enum Status { |
|
18 |
none, started, commit, rollback |
|
19 |
}; |
|
20 |
|
|
21 |
public enum TranResult { |
|
22 |
none, notCommit, commitOK, rollback, commitNoDone |
|
23 |
} |
|
24 |
|
|
25 |
public enum Trantype { |
|
26 |
func, service |
|
27 |
} |
|
28 |
|
|
29 |
private Status status = Status.none; |
|
30 |
private TranResult result = TranResult.none; |
|
31 |
private Transaction transaction = null; |
|
32 |
private Session session = null; |
|
33 |
private int sCount = 0; |
|
34 |
private SessionFactory sf; |
|
35 |
|
|
36 |
public TransactionContext(SessionFactory sf) { |
|
37 |
this.sf = sf; |
|
38 |
} |
|
39 |
public Session getsession() { |
|
40 |
if (session != null && session.isOpen() && session.isConnected() |
|
41 |
&& Daomethod.checksession(session)) { |
|
42 |
return session; |
|
43 |
} |
|
44 |
this.session = sf.openSession(); |
|
45 |
return session; |
|
46 |
} |
|
47 |
public Status checkStatus() { |
|
48 |
return status; |
|
49 |
} |
|
50 |
|
|
51 |
public TranResult checkResult() { |
|
52 |
return result; |
|
53 |
} |
|
54 |
|
|
55 |
public Transaction getTransaction() { |
|
56 |
return transaction; |
|
57 |
} |
|
58 |
|
|
59 |
public void setTransaction(Transaction transaction) { |
|
60 |
this.transaction = transaction; |
|
61 |
} |
|
62 |
|
|
63 |
public Status startTran() { |
|
64 |
this.session = this.getsession(); |
|
65 |
this.transaction = session.getTransaction(); |
|
66 |
if(sCount>0){ |
|
67 |
if (this.transaction != null && this.transaction .isActive()) { |
|
68 |
sCount++; |
|
69 |
return status; |
|
70 |
} |
|
71 |
} |
|
72 |
sCount = 0;// 如果是sCount小于0的话调整为0 |
|
73 |
try { |
|
74 |
this.transaction = session.beginTransaction(); |
|
75 |
if (transaction != null) { |
|
76 |
sCount++; |
|
77 |
this.status = Status.started; |
|
78 |
return this.status; |
|
79 |
} |
|
80 |
} catch (Exception e) { |
|
81 |
e.printStackTrace(); |
|
82 |
transaction = null; |
|
83 |
this.status = Status.none; |
|
84 |
this.close(); |
|
85 |
} |
|
86 |
return Status.none; |
|
87 |
} |
|
88 |
/** |
|
89 |
* 强制提交 |
|
90 |
* @return |
|
91 |
* @author mengly |
|
92 |
* @version 创建时间:2016年10月22日 下午12:17:27 |
|
93 |
*/ |
|
94 |
public TranResult commitF(){ |
|
95 |
if (this.session == null || !this.session.isOpen()) { |
|
96 |
result = TranResult.none; |
|
97 |
sCount = 0; |
|
98 |
return result; |
|
99 |
} |
|
100 |
try{ |
|
101 |
this.transaction=this.session.getTransaction(); |
|
102 |
this.transaction.commit(); |
|
103 |
return TranResult.commitOK; |
|
104 |
}catch(Exception e){ |
|
105 |
this.transaction.rollback(); |
|
106 |
return TranResult.rollback; |
|
107 |
}finally{ |
|
108 |
transaction=null; |
|
109 |
sCount = 0; |
|
110 |
} |
|
111 |
} |
|
112 |
public TranResult commit() { |
|
113 |
if (this.session == null || !this.session.isOpen()) { |
|
114 |
result = TranResult.none; |
|
115 |
sCount = 0; |
|
116 |
return result; |
|
117 |
} |
|
118 |
this.transaction=this.getsession().getTransaction(); |
|
119 |
sCount--; |
|
120 |
if (sCount <= 0) { |
|
121 |
try { |
|
122 |
if (this.transaction != null) { |
|
123 |
this.transaction.commit(); |
|
124 |
this.status = Status.commit; |
|
125 |
} |
|
126 |
this.transaction = null; |
|
127 |
return TranResult.commitOK; |
|
128 |
} catch (Exception e) { |
|
129 |
e.printStackTrace(); |
|
130 |
// if (this.transaction != null) { |
|
131 |
// this.transaction.rollback(); |
|
132 |
// this.transaction = null; |
|
133 |
// } |
|
134 |
return TranResult.rollback; |
|
135 |
} finally { |
|
136 |
this.close(); |
|
137 |
} |
|
138 |
} else { |
|
139 |
return TranResult.commitNoDone; |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
public int getSCount(){ |
|
144 |
return this.sCount; |
|
145 |
} |
|
146 |
public TranResult rollback() { |
|
147 |
this.transaction=this.getsession().getTransaction(); |
|
148 |
try { |
|
149 |
if(this.transaction!=null) |
|
150 |
this.transaction.rollback(); |
|
151 |
this.transaction = null; |
|
152 |
this.status = Status.rollback; |
|
153 |
this.sCount = 0; |
|
154 |
return TranResult.rollback; |
|
155 |
} catch (Exception e) { |
|
156 |
e.printStackTrace(); |
|
157 |
} finally { |
|
158 |
this.sCount = 0; |
|
159 |
this.transaction = null; |
|
160 |
this.close(); |
|
161 |
} |
|
162 |
return result; |
|
163 |
} |
|
164 |
|
|
165 |
public void close() { |
|
166 |
if (this.session != null) { |
|
167 |
try { |
|
168 |
this.status = Status.none; |
|
169 |
this.transaction=this.session.getTransaction(); |
|
170 |
if(transaction!=null&&transaction.isActive()){ |
|
171 |
transaction.rollback(); |
|
172 |
} |
|
173 |
this.sCount = 0; |
|
174 |
this.transaction = null; |
|
175 |
session.close(); |
|
176 |
} catch (Exception e) { |
|
177 |
this.sCount = 0; |
|
178 |
this.transaction = null; |
|
179 |
session.close(); |
|
180 |
} |
|
181 |
} |
|
182 |
} |
|
183 |
} |