Subject Re: Datasources and JNDI
Author Roman Rokytskyy
> Can you give an example please?

Here's the part of the JUnit test suite
(client-java/src/test/org/firebirdsql/pool/TestFBConnectionPoolDataSource.java):

/**
* Test correctness of JNDI serialization/deserialization.
*
* @throws Exception if something went wrong.
*/
public void testJNDI() throws Exception {
String JNDI_FACTORY =
"com.sun.jndi.fscontext.RefFSContextFactory";

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
props.put(Context.OBJECT_FACTORIES,
FBConnectionPoolDataSource.class.getName());

Context context = new InitialContext(env);
try {
context.bind("jdbc/test", pool);

BasicAbstractConnectionPool testPool =
(BasicAbstractConnectionPool)context.lookup("jdbc/test");

Connection testConnection =
testPool.getPooledConnection().getConnection();
try {
Statement stmt = testConnection.createStatement();
try {
ResultSet rs = stmt.executeQuery("SELECT 1 FROM
rdb$database");
assertTrue("Result set should have at least one
row.", rs.next());
assertTrue("Should return correct value",
rs.getInt(1) == 1);
assertTrue("Result set should have only one row.",
!rs.next());
} finally {
stmt.close();
}
} finally {
testConnection.close();
}

} finally {
context.unbind("jdbc/test");
}
}

public void testReferenceSupport() throws Exception {
Reference ref = new
Reference(FBWrappingDataSource.class.getName());

// Firebird standard properties
ref.add(new StringRefAddr("database", DB_DATASOURCE_URL));
ref.add(new StringRefAddr("userName", DB_USER));
ref.add(new StringRefAddr("password", DB_PASSWORD));
ref.add(new StringRefAddr("type", getGdsType().toString()));
ref.add(new StringRefAddr("sqlRole", "USER"));
ref.add(new StringRefAddr("blobBufferSize", "32767"));
ref.add(new StringRefAddr("socketBufferSize", "8192"));

// pool properties
ref.add(new StringRefAddr("blockingTimeout", "1000"));
ref.add(new StringRefAddr("idleTimeout", "1000"));
ref.add(new StringRefAddr("retryInterval", "100"));
ref.add(new StringRefAddr("maxConnections", "5"));
ref.add(new StringRefAddr("minConnections", "2"));
ref.add(new StringRefAddr("pooling", "false"));
ref.add(new StringRefAddr("statementPooling", "false"));
ref.add(new StringRefAddr("pingStatement", "SELECT CAST(2 AS
INTEGER) FROM RDB$DATABASE"));
ref.add(new StringRefAddr("pingInterval", "12000"));
ref.add(new StringRefAddr("isolation",
"TRANSACTION_REPEATABLE_READ"));

// non-standard properties
ref.add(new StringRefAddr("nonStandard",
"isc_dpb_set_db_charset : WIN1251"));
ref.add(new StringRefAddr("nonStandard", "isc_dpb_num_buffers
2048"));
ref.add(new StringRefAddr("isc_dpb_sweep_interval", "100"));

String JNDI_FACTORY =
"com.sun.jndi.fscontext.RefFSContextFactory";

Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
props.put(Context.OBJECT_FACTORIES,
FBWrappingDataSource.class.getName());

Context ctx = new InitialContext(props);
try {
ctx.bind("jdbc/test", ref);

Object obj = ctx.lookup("jdbc/test");

assertTrue("Should provide correct data source", obj
instanceof FBWrappingDataSource);

FBWrappingDataSource ds = (FBWrappingDataSource)obj;

assertEquals(DB_DATASOURCE_URL, ds.getDatabase());
assertEquals(DB_USER, ds.getUserName());
assertEquals(DB_PASSWORD, ds.getPassword());
assertEquals(getGdsType().toString(), ds.getType());
assertEquals("USER", ds.getSqlRole());
assertEquals(32767, ds.getBlobBufferSize());
assertEquals(8192, ds.getSocketBufferSize());

assertEquals(1000, ds.getBlockingTimeout());
assertEquals(1000, ds.getMaxIdleTime());
assertEquals(5, ds.getMaxPoolSize());
assertEquals(2, ds.getMinConnections());
assertEquals(12000, ds.getPingInterval());
assertEquals("TRANSACTION_REPEATABLE_READ",
ds.getIsolation());

// These properties are not avaiable via
// FBWrappingDataSource interface
//
//assertEquals(100, ds.getRetryInterval());
//assertEquals(false, ds.getPooling());
//assertEquals(false, ds.getStatementPooling());
//assertEquals("SELECT CAST(2 AS INTEGER) FROM
//RDB$DATABASE", ds.getPingStatement());

assertEquals("WIN1251",
ds.getNonStandardProperty("isc_dpb_set_db_charset"));
assertEquals("2048",
ds.getNonStandardProperty("isc_dpb_num_buffers"));
assertEquals("100",
ds.getNonStandardProperty("isc_dpb_sweep_interval"));
} finally {
ctx.unbind("jdbc/test");
}
}