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;
}
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());
}
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();
}
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");
}
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();
}
Aggregations