forked from hazelcast/hazelcast-jet-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JdbcXaTest.java
99 lines (85 loc) · 3.32 KB
/
JdbcXaTest.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
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.jet.contrib.xatests;
import javax.sql.XAConnection;
import javax.sql.XADataSource;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
//import org.postgresql.xa.PGXADataSource;
/**
* Tests if the JDBC database persists a prepared XA transaction when the
* client disconnects.
* <p>
* You need to add your XADataSource to getXADataSource() method.
*/
public final class JdbcXaTest {
private JdbcXaTest() { }
/**
* Configure XADataSource for broker here.
*/
private static XADataSource getXADataSource() {
// Add your datasource here, here is an example for PostgreSQL:
// PGXADataSource factory = new PGXADataSource();
// factory.setUrl(URL);
// factory.setUser(USER);
// factory.setPassword(PASSWORD);
// factory.setDatabaseName(DATABASE_NAME);
// return factory;
return null;
}
/** */
public static void main(String[] args) throws Exception {
// replace this line with a factory for your database, for example:
// PGXADataSource factory = new PGXADataSource();
XADataSource factory = getXADataSource();
if (factory == null) {
throw new IllegalArgumentException("Provide XADataSource for your broker in the getXADataSource() method");
}
// create an xa-connection, connection and XA transaction
XAConnection xaConn = factory.getXAConnection();
Connection conn = xaConn.getConnection();
XAResource xaRes = xaConn.getXAResource();
Xid xid = new MyXid(1);
// create a table "foo"
Statement stmt = conn.createStatement();
stmt.execute("create table foo(a numeric)");
// start the transaction and insert one record
xaRes.start(xid, XAResource.TMNOFLAGS);
stmt.execute("insert into foo values (1)");
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.prepare(xid);
// Now disconnect. Some brokers roll back the transaction, but this is not
// compatible with Jet's fault tolerance.
xaConn.close();
// connect again
xaConn = factory.getXAConnection();
conn = xaConn.getConnection();
xaRes = xaConn.getXAResource();
// commit the prepared transaction
xaRes.commit(xid, false);
// check that record is inserted
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select a from foo");
if (!rs.next() || rs.getInt(1) != 1) {
System.err.println("Failure: record missing or has wrong value");
} else {
System.out.println("Success!");
}
}
}