KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > XAPoolTestCase


1 import junit.framework.Test;
2 import junit.framework.TestSuite;
3 import junit.framework.TestCase;
4
5 import java.util.Properties JavaDoc;
6
7 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
8 import org.enhydra.jdbc.standard.StandardXADataSource;
9 import org.objectweb.jotm.Jotm;
10 import org.objectweb.transaction.jta.TMService;
11
12 import javax.naming.InitialContext JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.transaction.UserTransaction JavaDoc;
15 import java.util.Properties JavaDoc;
16 import java.sql.ResultSet JavaDoc;
17 import java.sql.PreparedStatement JavaDoc;
18 import java.sql.Connection JavaDoc;
19 import java.sql.Statement JavaDoc;
20
21 public class XAPoolTestCase extends TestCase {
22
23     public String JavaDoc login = null;
24     public String JavaDoc password = null;
25     public String JavaDoc url = null;
26     public String JavaDoc driver = null;
27     public TMService jotm;
28     public String JavaDoc USER_TRANSACTION_JNDI_NAME = "UserTransaction";
29     public UserTransaction JavaDoc utx = null;
30     public StandardXAPoolDataSource spds;
31     public StandardXADataSource xads;
32     public Connection JavaDoc conn;
33     public String JavaDoc SQL_REQUEST = "select id, foo from testdata where id=1";
34     public String JavaDoc SQL_QUERY = "update testdata set foo = ? where id=1";
35     public String JavaDoc datasourceClassName;
36     
37     public XAPoolTestCase(String JavaDoc strTestName) {
38     super(strTestName);
39     //System.out.println("New Object");
40
}
41     
42     public static Test suite() {
43     return new TestSuite(XAPoolTestSuite.class);
44     }
45
46     protected void setUp() throws Exception JavaDoc {
47     //System.out.println("setUp");
48
// first, load the properties from the spy.properties file
49
Properties JavaDoc prop = new Properties JavaDoc();
50         try {
51             prop.load(ClassLoader.getSystemResourceAsStream("spy.properties"));
52         } catch (Exception JavaDoc e) {
53             System.err.println("problem to load properties.");
54             e.printStackTrace();
55             throw e;
56         }
57     
58         login = prop.getProperty("login");
59         password = prop.getProperty("password");
60         url = prop.getProperty("url");
61         driver = prop.getProperty("driver");
62
63         // TML -Allow overriding the datasource class name in order to
64
// support Sybase, IDB, etc. Defaults to the Standard implementation
65
datasourceClassName = prop.getProperty("datasource-class",
66               StandardXADataSource.class.getName());
67         
68         // Get a transaction manager
69
try {
70             // creates an instance of JOTM with a local transaction factory which is not bound to a registry
71
jotm = new Jotm(true, false);
72             //InitialContext ictx = new InitialContext();
73
//ictx.rebind(USER_TRANSACTION_JNDI_NAME, jotm.getUserTransaction());
74
} catch (Exception JavaDoc e) {
75             System.err.println("JOTM problem.");
76             e.printStackTrace();
77             throw e;
78         }
79
80
81         try {
82             //Context ictx = new InitialContext();
83
//utx = (UserTransaction) ictx.lookup(USER_TRANSACTION_JNDI_NAME);
84
utx = jotm.getUserTransaction();
85         } catch (Exception JavaDoc e) {
86             System.err.println("Exception of type :" + e.getClass().getName() + " has been thrown");
87             System.err.println("Exception message :" + e.getMessage());
88             e.printStackTrace();
89             throw e;
90         }
91
92         // create an XA pool datasource with a minimum of 4 objects
93
spds = new StandardXAPoolDataSource(2);
94         spds.setMaxSize(15);
95         spds.setMinSize(13);
96         spds.setUser(login);
97         spds.setPassword(password);
98
99         // create an XA datasource which will be given to the XA pool
100
Class JavaDoc datasourceClass=Class.forName(datasourceClassName, true, getClass().getClassLoader());
101         xads = (StandardXADataSource)datasourceClass.newInstance();
102         try {
103             xads.setDriverName(driver);
104             xads.setUrl(url);
105             xads.setUser(login);
106             xads.setPassword(password);
107         } catch (Exception JavaDoc e) {
108             e.printStackTrace();
109             throw e;
110         }
111         spds.setTransactionManager(jotm.getTransactionManager());
112
113         // give the XA datasource to the pool (to create futur objects)
114
spds.setDataSource(xads);
115     }
116
117     public int getValue() {
118         try {
119         // some tests close the connection, here, we need to get it
120
// and do not forget to close it after use !!!
121
boolean toClose = false;
122         if (conn.isClosed()) {
123         toClose = true;
124         conn = spds.getConnection(login, password);
125
126         }
127
128         Statement JavaDoc st = conn.createStatement();
129             ResultSet JavaDoc rset = st.executeQuery(SQL_REQUEST);
130             int numcols = rset.getMetaData().getColumnCount();
131         int res;
132             rset.next();
133         res = Integer.parseInt(rset.getString(2));
134         rset.close();
135             st.close();
136
137         // here, we need to close the connection because it was
138
// closed at the beginning of the method
139
if (toClose)
140         conn.close();
141         return res;
142         } catch (Exception JavaDoc e) {
143             e.printStackTrace();
144         }
145     return 0;
146     }
147     
148     protected void tearDown() throws Exception JavaDoc {
149     //System.out.println("tearDown");
150
try {
151         //InitialContext ictx = new InitialContext();
152
//ictx.unbind(USER_TRANSACTION_JNDI_NAME);
153
} catch (Exception JavaDoc e) {
154            throw e;
155         }
156         jotm.stop();
157         jotm = null;
158     spds.stopPool();
159     xads = null;
160     spds = null;
161     
162     }
163
164 }
165
Popular Tags