You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Ondrej Tisler (JIRA)" <ji...@apache.org> on 2009/11/26 15:13:39 UTC

[jira] Created: (DBCP-309) First example for FSContext is invalid

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.


[jira] Updated: (DBCP-309) First example for FSContext is invalid

Posted by "Phil Steitz (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/DBCP-309?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Phil Steitz updated DBCP-309:
-----------------------------

    Fix Version/s: 1.4.1
                   1.3.1

> 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
>             Fix For: 1.3.1, 1.4.1
>
>
> 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.


[jira] Commented: (DBCP-309) First example for FSContext is invalid

Posted by "Phil Steitz (JIRA)" <ji...@apache.org>.
    [ 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.


[jira] Commented: (DBCP-309) First example for FSContext is invalid

Posted by "Ondrej Tisler (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/DBCP-309?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12784807#action_12784807 ] 

Ondrej Tisler commented on DBCP-309:
------------------------------------

I use this code for registring JNDI inFSContext:
      // JNDI with datasource
      InitialContext ic = new InitialContext();
      // Construct BasicDataSource reference
      Configuration poolConfig = dbConfig.subset("datasource");
      DriverAdapterCPDS cpds = new DriverAdapterCPDS();
      cpds.setDriver(poolConfig.getString("driver.driverClassName"));
      cpds.setUrl(poolConfig.getString("driver.url"));
      cpds.setUser(poolConfig.getString("driver.username"));
      cpds.setPassword(CryptPass.encryptPass(poolConfig.getString("driver.password")));
      SharedPoolDataSource ds = new SharedPoolDataSource();
      ds.setConnectionPoolDataSource(cpds);
      ds.setDefaultAutoCommit(poolConfig.getBoolean("connection.defaultAutoCommit", true));
      ds.setValidationQuery(poolConfig.getString("validation.validationQuery"));
      ic.rebind(poolConfig.getString("[@jndi]"), ds);
      log.info("JNDI datasource is inicialized");

next you can normaly use JNDI lookup:
      InitialContext ic2 = new InitialContext();
      ds = (DataSource) ic2.lookup(poolConfig.getString("[@jndi]"));
      conn = ds.getConnection();

> 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.


[jira] Commented: (DBCP-309) First example for FSContext is invalid

Posted by "Phil Steitz (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/DBCP-309?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12783483#action_12783483 ] 

Phil Steitz commented on DBCP-309:
----------------------------------

Looks like a bug in the examples to me.  Looks like the BasicDataSource example is taken from an example (Tomcat?) where a more general type of resource is being created but not bound directly into the JNDI context.  Unless I am missing something, it would be simpler (and correct) to just create the datasource instance and bind it directly.

> 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.