summaryrefslogtreecommitdiff
path: root/src/interfaces/jdbc/org/postgresql/xa/Test.java
blob: 193f0b18ae54547d7139a3856930b97fadf605ff (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package org.postgresql.xa;


import java.sql.*;
import javax.sql.*;
import javax.transaction.xa.*;


public class Test
{


    public static void main( String args[] )
    {
	XADataSource           xaDS;
	java.io.PrintWriter    log;

	log = new java.io.PrintWriter( System.out );
	try {

	    xaDS = new XADataSource();
	    xaDS.setDatabaseName( "test" );
	    xaDS.setUser( "arkin" );
	    xaDS.setPassword( "natasha" );
	    xaDS.setLogWriter( log );

	    Thread1 thread1;

	    thread1 = new Thread1();
	    thread1.xaConn = xaDS.getXAConnection();
	    thread1.xid1 = new XidImpl();

	    Thread2 thread2;

	    thread2 = new Thread2();
	    thread1.thread2 = thread2;
	    thread2.thread1 = thread1;
	    thread2.xaConn = xaDS.getXAConnection();
	    thread2.xid1 = thread1.xid1;
	    thread2.xid2 = new XidImpl();
	    
	    thread1.start();
	    thread2.start();
	    
	} catch ( Exception except ) {
	    System.out.println( except );
	    except.printStackTrace();
	}
	log.flush();
    }


}


class Thread1
    extends Thread
{


    public void run()
    {
	Connection conn;
	XAResource xaRes;
	Statement stmt;
	ResultSet rs;

	try {
	    conn  = xaConn.getConnection();
	    xaRes = xaConn.getXAResource();
	} catch ( Exception except ) {
	    System.out.println( except );
	    return;
	}
	// Initially the table should have no value.
	try {
	    stmt = conn.createStatement();
	    stmt.executeUpdate( "update test set text='nothing' where id=1" );
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}


	// Begin a transaction on this connection.
	// Perform an update on the table.
	System.out.println( "[Thread1] Starting transaction" );
	try {
	    xaRes.start( xid1, XAResource.TMNOFLAGS );
	} catch ( XAException except ) {
	    System.out.println( except );
	    return;	    
	}
	System.out.println( "[Thread1] Updating table" );
	try {
	    stmt = conn.createStatement();
	    stmt.executeUpdate( "update test set text='first' where id=1" );
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}


	// Thread2 will start a new transction and attempt
	// to perform an update on the table and will lock.
	System.out.println( "[Thread1] Waking up Thread2" );
	thread2.interrupt();
	try {
	    sleep( Integer.MAX_VALUE );
	} catch ( InterruptedException except ) { }


	// Perform a select from the table just to prove
	// that Thread2 failed in its update.
	System.out.println( "[Thread1] Selecting from table" );
	try {
	    stmt = conn.createStatement();
	    rs = stmt.executeQuery( "select text from test where id=1" );
	    rs.next();
	    System.out.println( "First = " + rs.getString( 1 ) );
	    rs.close();
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}


	// Thread2 will now attempt to join our transaction
	// and perform an update on the table.
	System.out.println( "[Thread1] Waking up Thread2" );
	thread2.interrupt();
	try {
	    sleep( Integer.MAX_VALUE );
	} catch ( InterruptedException except ) { }


	// Perform a select from the table to prove that
	// Thread2 managed to update it.
	System.out.println( "[Thread1] Selecting from table" );
	try {
	    stmt = conn.createStatement();
	    rs = stmt.executeQuery( "select text from test where id=1" );
	    rs.next();
	    System.out.println( "First = " + rs.getString( 1 ) );
	    rs.close();
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}


	// We now end the transaction for this thread.
	// We are no longer in the shared transaction.
	// Perform an update on the table and the update
	// will lock.
	System.out.println( "[Thread1] Ending transaction" );
	try {
	    xaRes.end( xid1, XAResource.TMSUCCESS );
	} catch ( XAException except ) {
	    System.out.println( except );
	    return;	    
	}
	System.out.println( "[Thread1] Selecting from table" );
	try {
	    stmt = conn.createStatement();
	    stmt.executeUpdate( "update test set text='first' where id=1" );
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}


	// Thread 2 will now end the transcation and commit it.
	System.out.println( "[Thread1] Waking up Thread2" );
	thread2.interrupt();
	try {
	    sleep( Integer.MAX_VALUE );
	} catch ( InterruptedException except ) { }


	// Perform a select on the table to prove that it
	// was only updated inside the transaction.
	System.out.println( "[Thread1] Selecting from table" );
	try {
	    stmt = conn.createStatement();
	    rs = stmt.executeQuery( "select text from test where id=1" );
	    rs.next();
	    System.out.println( "First = " + rs.getString( 1 ) );
	    rs.close();
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}
    }


    javax.sql.XAConnection xaConn;


    Xid                    xid1;


    Thread                 thread2;


}


class Thread2
    extends Thread
{


    public void run()
    {
	Connection conn;
	XAResource xaRes;
	Statement stmt;
	ResultSet rs;

       
	try {
	    conn  = xaConn.getConnection();
	    xaRes = xaConn.getXAResource();
	} catch ( Exception except ) {
	    System.out.println( except );
	    return;
	}
	// Thread2 immediately goes to sleep, waits
	// for Thread1 to wake it up.
	try {
	    sleep( Integer.MAX_VALUE );
	} catch ( InterruptedException except ) { }


	// Begin a transaction on this connection.
	// Perform an update on the table. This will
	// lock since Thread1 is in a different transaction
	// updating the same table.
	System.out.println( "[Thread2] Starting transaction" );
	try {
	    xaRes.start( xid2, XAResource.TMNOFLAGS );
	} catch ( XAException except ) {
	    System.out.println( except );
	    return;	    
	}
	System.out.println( "[Thread2] Updating table" );
	try {
	    stmt = conn.createStatement();
	    stmt.executeUpdate( "update test set text='second' where id=1" );
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}


	// Thread1 will now proof that it owns the
	// transaction.
	System.out.println( "[Thread2] Waking up Thread1" );
	thread1.interrupt();
	try {
	    sleep( Integer.MAX_VALUE );
	} catch ( InterruptedException except ) { }


	// We will now join the transaction shared with
	// Thread1 and try to update the table again.
	System.out.println( "[Thread2] Dumping transaction" );
	try {
	    xaRes.end( xid2, XAResource.TMFAIL );
	    // xaRes.rollback( xid2 );
	    xaRes.forget( xid2 );
	} catch ( XAException except ) {
	    System.out.println( except );
	    return;	    
	}
	System.out.println( "[Thread2] Joining transaction of Thread1" );
	try {
	    xaRes.start( xid1, XAResource.TMJOIN );
	} catch ( XAException except ) {
	    System.out.println( except );
	    return;	    
	}
	System.out.println( "[Thread2] Updating table" );
	try {
	    stmt = conn.createStatement();
	    stmt.executeUpdate( "update test set text='second' where id=1" );
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}


	// Thread1 will now proof that it could update
	// the table.
	System.out.println( "[Thread2] Waking up Thread1" );
	thread1.interrupt();
	try {
	    sleep( Integer.MAX_VALUE );
	} catch ( InterruptedException except ) { }


	// We will now end the transaction and commit it.
	System.out.println( "[Thread2] Commiting transaction" );
	try {
	    xaRes.end( xid1, XAResource.TMSUCCESS );
	    xaRes.prepare( xid1 );
	    xaRes.commit( xid1, false );
	    xaRes.forget( xid1 );
	} catch ( XAException except ) {
	    System.out.println( except );
	    return;	    
	}


	// Perform a select on the table to prove that it
	// was only updated inside the transaction.
	System.out.println( "[Thread2] Selecting from table" );
	try {
	    stmt = conn.createStatement();
	    rs = stmt.executeQuery( "select text from test where id=1" );
	    rs.next();
	    System.out.println( "First = " + rs.getString( 1 ) );
	    rs.close();
	    stmt.close();
	} catch ( SQLException except ) {
	    System.out.println( except );
	}


	// Thread1 will now proof that the table was only
	// updated inside the transaction. Thread 2 will die.
	System.out.println( "[Thread2] Waking up Thread1" );
	thread1.interrupt();
    }


    javax.sql.XAConnection xaConn;


    Xid                    xid1;


    Xid                    xid2;


    Thread                 thread1;


}



class XidImpl
    implements Xid
{
    
    
    public byte[] getBranchQualifier()
    {
	return null;
    }
    
    
    public byte[] getGlobalTransactionId()
    {
	return null;
    }
    
    
    public int getFormatId()
    {
	return 0;
    }
    
    
}