-
Notifications
You must be signed in to change notification settings - Fork 33
/
JDBCTxLogImpl.java
237 lines (196 loc) · 7.54 KB
/
JDBCTxLogImpl.java
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.bridge.service.jms.tx.log;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.io.ObjectInputStream;
import java.io.ByteArrayInputStream;
import com.sun.messaging.bridge.service.jms.tx.GlobalXid;
import com.sun.messaging.jmq.util.io.FilteringObjectInputStream;
import com.sun.messaging.bridge.service.jms.tx.BranchXid;
import com.sun.messaging.bridge.api.JMSBridgeStore;
import com.sun.messaging.bridge.api.UpdateOpaqueDataCallback;
import com.sun.messaging.bridge.service.jms.JMSBridge;
import com.sun.messaging.bridge.service.jms.resources.JMSBridgeResources;
/**
*
* @author amyk
*/
public class JDBCTxLogImpl extends TxLog {
private JMSBridgeStore _store = null;
private static JMSBridgeResources _jbr = JMSBridge.getJMSBridgeResources();
/** @throws IllegalArgumentException */
public void setJDBCStore(JMSBridgeStore store) {
if (store == null) {
throw new IllegalArgumentException("null JDBC store");
}
_store = store;
}
/**
* @param props The properties is guaranteed to contain "txlogDir", "txlogSuffix", "txlogMaxBranches", "jmsbridge"
* @param reset true to reset the txlog
*/
@Override
public void init(Properties props, boolean reset) throws Exception {
if (_store == null) {
throw new IllegalStateException("JDBC store is null");
}
if (_logger == null) {
throw new IllegalArgumentException("logger not set");
}
_jmsbridge = props.getProperty("jmsbridge");
if (_jmsbridge == null) {
throw new IllegalArgumentException("Property 'jmsbridge' not set");
}
_tmname = props.getProperty("tmname");
if (_tmname == null) {
throw new IllegalStateException("Property 'tmname' not set");
}
super.init(props, reset);
}
/**
* @param lr the LogRecord to log
*/
@Override
public void logGlobalDecision(LogRecord lr) throws Exception {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "jdbcTxLog: log global decision " + lr);
}
super.checkClosedAndSetInProgress();
try {
_store.storeTMLogRecord(lr.getGlobalXid().toString(), lr.toBytes(), _jmsbridge, false, _logger);
} finally {
super.setInProgress(false);
}
}
/**
* @param bxid the branch xid to update
* @param lr the LogRecord to identify the record to update
*/
@Override
public void logHeuristicBranch(BranchXid bxid, LogRecord lr) throws Exception {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "jdbcTxLog: log branch heuristic decision " + lr);
}
final GlobalXid gxid = lr.getGlobalXid();
final BranchXid branchXid = bxid;
final LogRecord newlr = lr;
super.checkClosedAndSetInProgress();
try {
UpdateOpaqueDataCallback callback = new UpdateOpaqueDataCallback() {
@Override
public Object update(Object currlr) throws Exception {
ObjectInputStream ois = new FilteringObjectInputStream(new ByteArrayInputStream((byte[]) currlr));
LogRecord oldlr = (LogRecord) ois.readObject();
if (oldlr == null) {
throw new IllegalArgumentException("Unexpected null current log record for " + gxid);
}
if (!oldlr.getGlobalXid().equals(gxid)) {
throw new IllegalArgumentException("Unexpected global xid " + oldlr.getGlobalXid() + " from store, expected:" + gxid);
}
ois.close();
if (oldlr.getBranchDecision(branchXid) == newlr.getBranchDecision(branchXid)) {
return currlr;
}
oldlr.setBranchDecision(branchXid, newlr.getBranchDecision(branchXid));
return oldlr;
}
};
_store.updateTMLogRecord(gxid.toString(), lr.toBytes(), _jmsbridge, callback, true, true, _logger);
} finally {
super.setInProgress(false);
}
}
/**
* @param gxid the global xid record to remove
*/
@Override
public void reap(String gxid) throws Exception {
String key = gxid;
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "jdbcTxLog: remove global transaction xid " + key);
}
super.checkClosedAndSetInProgress();
try {
_store.removeTMLogRecord(key, _jmsbridge, true, _logger);
} finally {
super.setInProgress(false);
}
}
/**
*
* @param gxid the GlobalXid
* @return a copy of LogRecord corresponding gxid or null if not exist
*/
@Override
public LogRecord getLogRecord(GlobalXid gxid) throws Exception {
String key = gxid.toString();
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "jdbcTxLog: get log record for xid " + key);
}
super.checkClosedAndSetInProgress();
try {
byte[] data = _store.getTMLogRecord(key, _jmsbridge, _logger);
if (data == null) {
return null;
}
ObjectInputStream ois = new FilteringObjectInputStream(new ByteArrayInputStream(data));
LogRecord lr = (LogRecord) ois.readObject();
ois.close();
return lr;
} finally {
super.setInProgress(false);
}
}
@Override
public List getAllLogRecords() throws Exception {
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "jdbcTxLog: get all log records");
}
ArrayList<LogRecord> list = new ArrayList<>();
super.checkClosedAndSetInProgress();
try {
List abytes = _store.getTMLogRecordsByName(_jmsbridge, _logger);
if (abytes == null) {
return list;
}
byte[] data = null;
Iterator<byte[]> itr = abytes.iterator();
while (itr.hasNext()) {
data = itr.next();
ObjectInputStream ois = new FilteringObjectInputStream(new ByteArrayInputStream(data));
LogRecord lr = (LogRecord) ois.readObject();
ois.close();
list.add(lr);
}
return list;
} finally {
super.setInProgress(false);
}
}
/**
*/
@Override
public void close() throws Exception {
_logger.log(Level.INFO, _jbr.getString(_jbr.I_JDBCTXNLOG_CLOSE));
super.setClosedAndWait();
super.close();
}
}