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