Example
To understand the concepts of JTX its best to see an example. To make things simpler, our transactional resource will be a simple String value. Let's create one such class:
1
public class WorkSession {
2
​
3
static String persistedValue = "jodd";
4
String sessionValue;
5
boolean readOnly;
6
int txno; // transaction number
7
​
8
public WorkSession() { // start session in non-tx mode
9
}
10
public WorkSession(int txno) { // start tx session
11
this.txno = txno;
12
}
13
​
14
public void writeValue(String value) {
15
if (txno == 0) { // no transaction
16
persistedValue = value;
17
return;
18
}
19
// under transaction
20
if (readOnly == true) {
21
throw new UncheckedException();
22
}
23
sessionValue = value;
24
}
25
​
26
public String readValue() {
27
if (sessionValue != null) {
28
return sessionValue;
29
}
30
return persistedValue;
31
}
32
​
33
// commit
34
public void done() {
35
if (sessionValue != null) {
36
persistedValue = sessionValue;
37
}
38
sessionValue = null;
39
}
40
​
41
// rollback
42
public void back() {
43
sessionValue = null;
44
}
Copied!
Now we are ready to begin our journey:)

ResourceManager

The first thing is to create a JtxResourceManager for our resource. The main method to implement is beginTransaction(). It starts a transaction on our resource depending on transaction mode.
Since we will use JtxTransactionManager, propagation behavior and timeout will be already supported! Therefore, our resource manager has only to deal with isolation and read-only attribute. We will ignore isolation to make things simpler. Here is how resource manager may look like:
1
public class WorkResourceManager implements JtxResourceManager<WorkSession> {
2
​
3
int txno = 1;
4
​
5
public Class<WorkSession> getResourceType() {
6
return WorkSession.class;
7
}
8
​
9
public WorkSession beginTransaction(JtxTransactionMode jtxMode, boolean active) {
10
if (active == false) {
11
return new WorkSession();
12
}
13
WorkSession work = new WorkSession(txno++);
14
work.readOnly = jtxMode.isReadOnly();
15
return work;
16
}
17
​
18
public void commitTransaction(WorkSession resource) {
19
resource.done();
20
txno--;
21
}
22
​
23
public void rollbackTransaction(WorkSession resource) {
24
resource.back();
25
txno--;
26
}
27
​
28
public void close() {
29
}
30
}
Copied!
Quick overview of what we have done in beginTransaction()\: the active flag tells us if real transaction should be started or we are working in auto-commit mode. When it is set, we create a transaction-aware resource. Since isolation is ignored, we only need to pass read-only flag.

Usage

Now we are ready to use JTX\:
1
// [1] create jtx manager and register our resource manager
2
JtxTransactionManager jtxManager = new JtxTransactionManager();
3
jtxManager.registerResourceManager(new WorkResourceManager());
4
​
5
// [2] request jtx
6
JtxTransaction jtx = manager.requestTransaction(
7
new JtxTransactionMode().propagationRequired().readOnly(true));
8
​
9
// [3] requrest resource i.e. start jtx
10
WorkSession work = jtx.requestResource(WorkSession.class);
11
​
12
// [4] work
13
work.writeValue("new value");
14
​
15
// [5] done
16
jtx.commit();
17
​
18
// [6] cleanup
19
manager.close();
Copied!
The most important thing to remember is that in step #2 we are just requesting a jtx transaction. Not until the next step, #3, the real transaction will be started. Again, we are requesting a resource, therefore, if we call it several time in a row, the same resource instance will be returned.

Worker

JTX also provides LeanJtxWorker, a class that utilizes JtxTransactionManager and makes it more convenient for use when transaction is requested over different context, i.e. with transaction nesting. Basically, everything stays the same, except LeanJtxWorker would return null when new transaction is not created on its request, meaning that current transaction matches the requested transaction attributes (mostly propagation).
Last modified 8mo ago
Export as PDF
Copy link