Search in sources :

Example 51 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project h2database by h2database.

the class OsgiDataSourceFactory method createDataSource.

/**
 * Creates a basic data source.
 *
 * @param properties the properties for the data source.
 * @throws SQLException if unsupported properties are supplied, or if data
 *             source can not be created.
 * @return a new data source.
 */
@Override
public DataSource createDataSource(Properties properties) throws SQLException {
    // Make copy of properties
    Properties propertiesCopy = new Properties();
    if (properties != null) {
        propertiesCopy.putAll(properties);
    }
    // Verify that no unsupported standard options are used
    rejectUnsupportedOptions(propertiesCopy);
    // Standard pool properties in OSGi not applicable here
    rejectPoolingOptions(propertiesCopy);
    JdbcDataSource dataSource = new JdbcDataSource();
    setupH2DataSource(dataSource, propertiesCopy);
    return dataSource;
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Properties(java.util.Properties)

Example 52 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project h2database by h2database.

the class TestDataSource method testDataSourceFactory.

private void testDataSourceFactory() throws Exception {
    ObjectFactory factory = new JdbcDataSourceFactory();
    assertTrue(null == factory.getObjectInstance("test", null, null, null));
    Reference ref = new Reference("java.lang.String");
    assertTrue(null == factory.getObjectInstance(ref, null, null, null));
    ref = new Reference(JdbcDataSource.class.getName());
    ref.add(new StringRefAddr("url", "jdbc:h2:mem:"));
    ref.add(new StringRefAddr("user", "u"));
    ref.add(new StringRefAddr("password", "p"));
    ref.add(new StringRefAddr("loginTimeout", "1"));
    ref.add(new StringRefAddr("description", "test"));
    JdbcDataSource ds = (JdbcDataSource) factory.getObjectInstance(ref, null, null, null);
    assertEquals(1, ds.getLoginTimeout());
    assertEquals("test", ds.getDescription());
    assertEquals("jdbc:h2:mem:", ds.getURL());
    assertEquals("u", ds.getUser());
    assertEquals("p", ds.getPassword());
    Reference ref2 = ds.getReference();
    assertEquals(ref.size(), ref2.size());
    assertEquals(ref.get("url").getContent().toString(), ref2.get("url").getContent().toString());
    assertEquals(ref.get("user").getContent().toString(), ref2.get("user").getContent().toString());
    assertEquals(ref.get("password").getContent().toString(), ref2.get("password").getContent().toString());
    assertEquals(ref.get("loginTimeout").getContent().toString(), ref2.get("loginTimeout").getContent().toString());
    assertEquals(ref.get("description").getContent().toString(), ref2.get("description").getContent().toString());
    ds.setPasswordChars("abc".toCharArray());
    assertEquals("abc", ds.getPassword());
}
Also used : JdbcDataSourceFactory(org.h2.jdbcx.JdbcDataSourceFactory) ObjectFactory(javax.naming.spi.ObjectFactory) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) JdbcDataSource(org.h2.jdbcx.JdbcDataSource)

Example 53 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project h2database by h2database.

the class TestDataSource method testXAConnection.

private void testXAConnection(boolean userInDataSource) throws Exception {
    deleteDb("dataSource");
    JdbcDataSource ds = new JdbcDataSource();
    String url = getURL("dataSource", true);
    String user = getUser();
    ds.setURL(url);
    if (userInDataSource) {
        ds.setUser(user);
        ds.setPassword(getPassword());
    }
    if (userInDataSource) {
        assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=" + user, ds.toString());
    } else {
        assertEquals("ds" + ds.getTraceId() + ": url=" + url + " user=", ds.toString());
    }
    XAConnection xaConn;
    if (userInDataSource) {
        xaConn = ds.getXAConnection();
    } else {
        xaConn = ds.getXAConnection(user, getPassword());
    }
    int traceId = ((JdbcXAConnection) xaConn).getTraceId();
    assertTrue(xaConn.toString().startsWith("xads" + traceId + ": conn"));
    xaConn.addConnectionEventListener(new ConnectionEventListener() {

        @Override
        public void connectionClosed(ConnectionEvent event) {
        // nothing to do
        }

        @Override
        public void connectionErrorOccurred(ConnectionEvent event) {
        // nothing to do
        }
    });
    XAResource res = xaConn.getXAResource();
    assertFalse(res.setTransactionTimeout(1));
    assertEquals(0, res.getTransactionTimeout());
    assertTrue(res.isSameRM(res));
    assertFalse(res.isSameRM(null));
    Connection conn = xaConn.getConnection();
    assertEquals(user.toUpperCase(), conn.getMetaData().getUserName());
    Xid[] list = res.recover(XAResource.TMSTARTRSCAN);
    assertEquals(0, list.length);
    Statement stat = conn.createStatement();
    stat.execute("SELECT * FROM DUAL");
    conn.close();
    xaConn.close();
}
Also used : Statement(java.sql.Statement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) XAResource(javax.transaction.xa.XAResource) Xid(javax.transaction.xa.Xid) ConnectionEvent(javax.sql.ConnectionEvent) XAConnection(javax.sql.XAConnection) JdbcXAConnection(org.h2.jdbcx.JdbcXAConnection) ConnectionEventListener(javax.sql.ConnectionEventListener)

Example 54 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project h2database by h2database.

the class TestDataSource method test.

// public static void main(String... args) throws SQLException {
// 
// // first, need to start on the command line:
// // rmiregistry 1099
// 
// // System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
// "com.sun.jndi.ldap.LdapCtxFactory");
// System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
// "com.sun.jndi.rmi.registry.RegistryContextFactory");
// System.setProperty(Context.PROVIDER_URL, "rmi://localhost:1099");
// 
// JdbcDataSource ds = new JdbcDataSource();
// ds.setURL("jdbc:h2:test");
// ds.setUser("test");
// ds.setPassword("");
// 
// Context ctx = new InitialContext();
// ctx.bind("jdbc/test", ds);
// 
// DataSource ds2 = (DataSource)ctx.lookup("jdbc/test");
// Connection conn = ds2.getConnection();
// conn.close();
// }
@Override
public void test() throws Exception {
    if (config.traceLevelFile > 0) {
        TraceSystem sys = JdbcDataSourceFactory.getTraceSystem();
        sys.setFileName(getBaseDir() + "/test/trace");
        sys.setLevelFile(3);
    }
    testDataSourceFactory();
    testDataSource();
    testUnwrap();
    testXAConnection();
    deleteDb("dataSource");
}
Also used : TraceSystem(org.h2.message.TraceSystem)

Example 55 with JdbcDataSource

use of org.h2.jdbcx.JdbcDataSource in project h2database by h2database.

the class TestXA method testMixedXaNormal.

private void testMixedXaNormal() throws Exception {
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:test");
    ds.setUser("sa");
    ds.setPassword("");
    XAConnection xa = ds.getXAConnection();
    Connection c = xa.getConnection();
    assertTrue(c.getAutoCommit());
    MyXid xid = new MyXid();
    XAResource res = xa.getXAResource();
    res.start(xid, XAResource.TMNOFLAGS);
    assertFalse(c.getAutoCommit());
    res.end(xid, XAResource.TMSUCCESS);
    res.commit(xid, true);
    assertTrue(c.getAutoCommit());
    res.start(xid, XAResource.TMNOFLAGS);
    assertFalse(c.getAutoCommit());
    res.end(xid, XAResource.TMFAIL);
    res.rollback(xid);
    assertTrue(c.getAutoCommit());
    c.close();
    xa.close();
}
Also used : XAResource(javax.transaction.xa.XAResource) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) XAConnection(javax.sql.XAConnection)

Aggregations

JdbcDataSource (org.h2.jdbcx.JdbcDataSource)81 Connection (java.sql.Connection)24 Test (org.junit.Test)24 Before (org.junit.Before)15 XAResource (javax.transaction.xa.XAResource)13 DataSource (javax.sql.DataSource)9 Xid (javax.transaction.xa.Xid)9 SQLException (java.sql.SQLException)8 InitialContext (javax.naming.InitialContext)8 XAConnection (javax.sql.XAConnection)8 RecoveryModule (com.arjuna.ats.arjuna.recovery.RecoveryModule)7 CommitMarkableResourceRecordRecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.CommitMarkableResourceRecordRecoveryModule)7 XARecoveryModule (com.arjuna.ats.internal.jta.recovery.arjunacore.XARecoveryModule)7 XAResourceRecoveryHelper (com.arjuna.ats.jta.recovery.XAResourceRecoveryHelper)7 Statement (java.sql.Statement)7 BMScript (org.jboss.byteman.contrib.bmunit.BMScript)7 File (java.io.File)6 Enumeration (java.util.Enumeration)6 Properties (java.util.Properties)6 Vector (java.util.Vector)6