01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.jdbcx;
07:
08: import java.sql.Connection;
09: import java.sql.Statement;
10:
11: import javax.sql.ConnectionEvent;
12: import javax.sql.ConnectionEventListener;
13: import javax.sql.XAConnection;
14: import javax.transaction.xa.XAResource;
15: import javax.transaction.xa.Xid;
16:
17: import org.h2.jdbcx.JdbcDataSource;
18: import org.h2.test.TestBase;
19:
20: /**
21: * Tests DataSource and XAConnection.
22: */
23: public class TestDataSource extends TestBase {
24:
25: // public static void main(String[] args) throws Exception {
26: //
27: // // first, need to start on the command line:
28: // // rmiregistry 1099
29: //
30: // // System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
31: // "com.sun.jndi.ldap.LdapCtxFactory");
32: // System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
33: // "com.sun.jndi.rmi.registry.RegistryContextFactory");
34: // System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
35: //
36: // JdbcDataSource ds = new JdbcDataSource();
37: // ds.setURL("jdbc:h2:test");
38: // ds.setUser("test");
39: // ds.setPassword("");
40: //
41: // Context ctx = new InitialContext();
42: // ctx.bind("jdbc/test", ds);
43: //
44: // DataSource ds2 = (DataSource)ctx.lookup("jdbc/test");
45: // Connection conn = ds2.getConnection();
46: // conn.close();
47: // }
48:
49: public void test() throws Exception {
50: testDataSource();
51: testXAConnection();
52: }
53:
54: private void testXAConnection() throws Exception {
55: deleteDb(baseDir, "dataSource");
56: JdbcDataSource ds = new JdbcDataSource();
57: ds.setURL("jdbc:h2:" + baseDir + "/dataSource");
58: XAConnection xaConn = ds.getXAConnection();
59: xaConn
60: .addConnectionEventListener(new ConnectionEventListener() {
61: public void connectionClosed(ConnectionEvent event) {
62: }
63:
64: public void connectionErrorOccurred(
65: ConnectionEvent event) {
66: }
67: });
68: XAResource res = xaConn.getXAResource();
69: Connection conn = xaConn.getConnection();
70: Xid[] list = res.recover(XAResource.TMSTARTRSCAN);
71: check(list.length, 0);
72: Statement stat = conn.createStatement();
73: stat.execute("SELECT * FROM DUAL");
74: conn.close();
75: xaConn.close();
76: }
77:
78: private void testDataSource() throws Exception {
79: deleteDb(baseDir, "dataSource");
80: JdbcDataSource ds = new JdbcDataSource();
81: ds.setURL("jdbc:h2:" + baseDir + "/dataSource");
82: ds.setUser("sa");
83: Connection conn = ds.getConnection();
84: Statement stat = conn.createStatement();
85: stat.execute("SELECT * FROM DUAL");
86: conn.close();
87: }
88:
89: }
|