summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1-RC3/itest/transaction/src/test/java/org/apache/tuscany/sca/itest/transaction/ConcurrentXAResourceTestCase.java
blob: c23628ff9101f2121e89a2cc7bbc97250583ffda (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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.tuscany.sca.itest.transaction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

import javax.sql.XAConnection;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;

import junit.framework.TestCase;

import org.apache.derby.jdbc.EmbeddedXADataSource;
import org.apache.tuscany.sca.policy.transaction.TransactionManagerHelper;
import org.apache.tuscany.sca.policy.transaction.TransactionManagerWrapper;

public class ConcurrentXAResourceTestCase extends TestCase {
    class TestThread extends Thread {
        private int counter;

        TestThread(int i) {
            counter = i;
        }

        public void run() {
            log.info(this + " running...");

            EmbeddedXADataSource xads = new EmbeddedXADataSource();
            xads.setDatabaseName("target/test" + counter);
            xads.setCreateDatabase("create");
            Connection conn = null;
            try {

                Transaction trans = helper.managedGlobalTransactionPreInvoke();

                XAConnection xaconn = xads.getXAConnection();

                trans.enlistResource(xaconn.getXAResource());

                conn = xaconn.getConnection();
                try {
                    conn.prepareStatement("create table T1(col1 char(100))").execute();
                } catch (SQLException ex) {
                    log.info("table T1 exists");
                }
                conn.prepareStatement("insert into T1 values('kkkkkkkk')").execute();
                ResultSet rs = conn.prepareStatement("select count(*) from T1").executeQuery();
                rs.next();
                log.info(String.valueOf(rs.getInt(1)));

                helper.managedGlobalTransactionPostInvoke(trans, false);

            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    conn.close();
                } catch (Exception _ex) {
                    _ex.printStackTrace();
                }
            }
        }

    }
    private TransactionManagerWrapper activator;
    private TransactionManagerHelper helper;

    private Logger log = Logger.getLogger(this.getClass().getName());

    public void setUp() throws Exception {
        activator = new TransactionManagerWrapper();
        activator.start();
        TransactionManager tm = activator.getTransactionManager();
        helper = new TransactionManagerHelper(tm);
    }

    public void tearDown() throws Exception {
        activator.stop();

    }

    public void testConcurrent() {
        TestThread[] tts = new TestThread[5];
        for (int i = 0; i < tts.length; i++) {
            tts[i] = new TestThread(i);
            tts[i].start();
            // log.info("one TestThread started...");
        }
        try {
            for (TestThread tt : tts) {
                while (tt != null && tt.isAlive()) {
                    // log.info("wait for...");
                    Thread.sleep(200);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}