[
https://issues.apache.org/jira/browse/DBCP-309?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12783729#action_12783729
]
Phil Steitz commented on DBCP-309:
----------------------------------
Strike last comment. DataSoources do not implement Referenceable, so direct
references will not work. When using the FileSystem provider, datasources are
created new for each lookup (as reported in the issue). Tomcat's in-memory
provider, however, returns the same datasource for repeated lookups. The
following test, for example, succeeds with the Tomcat provider, but fails with
the FS provider
{code}
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY,
org.apache.naming.java.javaURLContextFactory.class.getName());
InitialContext ic = new InitialContext(environment);
ic.createSubcontext("jdbc");
// Construct BasicDataSource reference
Reference ref = new Reference("javax.sql.DataSource",
"org.apache.commons.dbcp.BasicDataSourceFactory", null);
ref.add(new StringRefAddr("driverClassName", "TesterDriver"));
ref.add(new StringRefAddr("url", "jdbc:apache:commons:testdriver"));
ref.add(new StringRefAddr("username", "username"));
ref.add(new StringRefAddr("password", "password"));
ic.rebind("jdbc/basic", ref);
// Use
InitialContext ic2 = new InitialContext(environment);
DataSource ds = (DataSource) ic2.lookup("jdbc/basic");
((BasicDataSource) ds).setAccessToUnderlyingConnectionAllowed(true);
Assert.assertNotNull(ds);
Connection conn = ds.getConnection();
Connection uconn = ((DelegatingConnection) conn).getInnermostDelegate();
Assert.assertNotNull(conn);
conn.close();
DataSource ds2 = (DataSource) ic2.lookup("jdbc/basic");
((BasicDataSource) ds2).setAccessToUnderlyingConnectionAllowed(true);
Assert.assertNotNull(ds2);
Connection conn2 = ds2.getConnection();
Connection uconn2 = ((DelegatingConnection)
conn2).getInnermostDelegate();
Assert.assertNotNull(conn2);
conn2.close();
Assert.assertEquals(ds, ds2);
Assert.assertEquals(uconn2, uconn);
{code}
To make the examples less misleading, it seems we have two choices - 1) change
to use the Tomcat provider or 2) comment that the FS provider creates new
datasources each time. Would appreciate comments / patches from JNDI spi
experts on this.
> First example for FSContext is invalid
> --------------------------------------
>
> Key: DBCP-309
> URL: https://issues.apache.org/jira/browse/DBCP-309
> Project: Commons Dbcp
> Issue Type: Bug
> Affects Versions: 1.2.2
> Reporter: Ondrej Tisler
> Priority: Trivial
>
> First example on page http://commons.apache.org/dbcp/guide/jndi-howto.html is
> invalid, with this code every call of
> InitialContext ic2 = new InitialContext();
> DataSource ds = (DataSource) ic2.lookup("jdbc/basic");
> Connection conn = ds.getConnection();
> conn.close();
> ends with new datasource with unclosed connection in it becase new
> Reference("javax.sql.DataSource",
> "org.apache.commons.dbcp.BasicDataSourceFactory", null); reference creates
> new DataSource instead using created one while calling
> ic2.lookup("jdbc/basic").
> At the end it ends with many opened connections to DB until JVM is ended.
> Second example I didn't test, i use direct aproach with manualy creating
> SharedPoolDataSource and registring it in FSContext JNDI itself.
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.