Hi,

Thanks guys.

"1. You have to wrap all this with try/finally and invoke close() on
ResultSet and Statement before calling that method on Connection
(which returns it to the pool, but does not actually close it). I'd
have resource leaks otherwise."

Cetainly, in this code I just wanted to test getConnection is not a real
application.


2. You need to set alternateUsernameAllowed="
>
> true" on Tomcat JDBC pool  [1]
> Otherwise arguments in ds.getConnection(user,password) method on that
> datasource are ignored.


Good, I'll test it. Anyway, the following description and example in
documentation is not valid (
tomcat.apache.org/tomcat-6.0-doc/config/context.html#Resource_Links,
tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Links):

"When the attribute
factory="org.apache.naming.factory.DataSourceLinkFactory" the resource link
can be used with two additional attributes to allow a shared data source to
be used with different credentials. When these two additional attributes
are used in combination with the javax.sql.DataSource type, different
contexts can share a global data source with different credentials. Under
the hood, what happens is that a call to
getConnection()<http://download.oracle.com/javase/6/docs/api/javax/sql/DataSource.html#getConnection%28%29>is
simply translated to a call getConnection(username,
password)<http://download.oracle.com/javase/6/docs/api/javax/sql/DataSource.html#getConnection%28java.lang.String,%20java.lang.String%29>on
the global data source. This is an easy way to get code to be
transparent to what schemas are being used, yet be able to control
connections (or pools) in the global configuration. "

<GlobalNamingResources>
  ...
  <Resource name="sharedDataSource"
            global="sharedDataSource"
            type="javax.sql.DataSource"
            username="bar"
            password="barpass"

            ...
  ...
</GlobalNamingResources>

<Context path="/foo"...>
  ...
  <ResourceLink
            name="appDataSource"
            global="sharedDataSource"
            type="javax.sql.DataSource"
            factory="org.apache.naming.factory.DataSourceLinkFactory"
            username="foo"
            password="foopass"
  ...
</Context>
<Context path="/bar"...>
  ...
  <ResourceLink
            name="appDataSource"
            global="sharedDataSource"
            type="javax.sql.DataSource"
  ...
</Context>



That way, just does not work.


Best regards,

Robert






On Wed, May 23, 2012 at 6:26 PM, Konstantin Kolinko
<knst.koli...@gmail.com>wrote:

> 2012/5/24 Robert Anderson <ranom...@gmail.com>:
> > Hi,
> >
> > I'm testing this functionality (versions 6.0.35 and 7.0.27) but it's not
> > working for me (https://issues.apache.org/bugzilla/show_bug.cgi?id=49543,
> >
> http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Resource_Links
> ).
> >
> >
> > -->server.xml
> >
> >
> >  <GlobalNamingResources>
> >
> >    <Resource name="jdbc/globalpg" auth="Container"
> > type="javax.sql.DataSource" removeAbandoned="true"
> > removeAbandonedTimeout="300"
> >                                   maxActive="400" maxIdle="30"
> > maxWait="10000"
> >                                   validationQuery="select 1"
> >                                   testOnBorrow="true"
> >
> > factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
> >                                   driverClassName="org.postgresql.Driver"
> >                                   url="jdbc:postgresql://
> > 172.17.1.5:5432/tjse"
> >                                   username="user1"
> >                                   password="validpassword"
> >                                   />
> >
> >  </GlobalNamingResources>
> >
> >
> > -->conf/Catalina/localhost/app1.xml
> >
> >
> > <Context>
> >
> >    <ResourceLink name="jdbc/localpg"
> >    global="jdbc/globalpg"
> >    type="javax.sql.DataSource"
> >    factory="org.apache.naming.factory.DataSourceLinkFactory"
> >    username="user2"
> >    password="invalidpassword"
> >    />
> >
> > </Context>
> >
> >
> > -->webapps/app1/index.jsp
> >
> > <?xml version="1.0" encoding="ISO-8859-1"?>
> > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> >   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";>
> > <%@ page session="false" import="javax.naming.*, java.sql.*,
> javax.sql.*" %>
> > <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
> >    <head>
> >    <title>Test shared data source</title>
> > </head>
> > <body>
> > <%
> >    Context ctx = new InitialContext();
> >    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/localpg");
> >    Connection c = ds.getConnection();
> >    Statement stm = c.createStatement();
> >
> >    ResultSet rs = stm.executeQuery("select 'this should fail because the
> > password of localpg is invalid but it is working.'");
> >
> >    rs.next();
> >
> > %>
> >    <%= rs.getString(1) %><br/>
> > <%
> >
> >    c.close();
> > %>
> > </body>
> > </html>
> >
> >
> > Am I doing some wrong?
> >
>
> 1. You have to wrap all this with try/finally and invoke close() on
> ResultSet and Statement before calling that method on Connection
> (which returns it to the pool, but does not actually close it). I'd
> have resource leaks otherwise.
>
> 2. You need to set alternateUsernameAllowed="true" on Tomcat JDBC pool  [1]
> Otherwise arguments in ds.getConnection(user,password) method on that
> datasource are ignored.
>
> [1] http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html
>
> Best regards,
> Konstantin Kolinko
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>

Reply via email to