Quantcast

Shared data source (Bug 49543)

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Shared data source (Bug 49543)

Robert Anderson
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?


Thanks in advance,


Robert
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Joao Silva
Hi

The ResourceLink element will use the parameters name, global and type.
Everything else is being ignored, as the resource link is but reference
to the global shared data source.
See http://tomcat.apache.org/tomcat-7.0-doc/config/context.html,
searching for ResourceLink.

Best regards

On 5/23/2012 9:31 PM, Robert Anderson wrote:

> 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?
>
>
> Thanks in advance,
>
>
> Robert
>

--
*João Silva
* *
VILT Group
* *_http://www.vilt-group.com
_
Rua Ivone Silva, 6 - 7º Esq
1050-124 Lisboa - Portugal

Mobile  +351 926 531 097
Phone   +351 210 34 33 00
Fax       +351 210 34 33 99
Email [hidden email] <mailto:[hidden email]>_*
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Konstantin Kolinko
In reply to this post by Robert Anderson
2012/5/24 Robert Anderson <[hidden email]>:

> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Robert Anderson
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
<[hidden email]>wrote:

> 2012/5/24 Robert Anderson <[hidden email]>:
> > 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: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Robert Anderson
In reply to this post by Joao Silva
Hi João,

This documentation also says:

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.DataSourcetype, 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.
AttributeDescriptionusername

username value for the getConnection(username, password) call on the linked
global DataSource.
password

password value for the getConnection(username, password) call on the linked
global DataSource.


Thanks,

Robert
On Wed, May 23, 2012 at 6:26 PM, Joao Silva <[hidden email]>wrote:

>  Hi
>
> The ResourceLink element will use the parameters name, global and type.
> Everything else is being ignored, as the resource link is but reference to
> the global shared data source.
> See http://tomcat.apache.org/tomcat-7.0-doc/config/context.html,
> searching for ResourceLink.
>
> Best regards
>
>
> On 5/23/2012 9:31 PM, Robert Anderson wrote:
>
> 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" <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" <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?
>
>
> Thanks in advance,
>
>
> Robert
>
>
>
> --
> *João Silva
> *  *
> VILT Group
>  *  *http://www.vilt-group.com
>
>  Rua Ivone Silva, 6 - 7º Esq
> 1050-124 Lisboa - Portugal
>
> Mobile  +351 926 531 097
> Phone   +351 210 34 33 00
> Fax       +351 210 34 33 99
> Email     [hidden email]*
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Konstantin Kolinko
In reply to this post by Robert Anderson
2012/5/24 Robert Anderson <[hidden email]>:

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

Can you be more specific? You are just citing a wall of text and it is
hard to digest.

Can you open an issue in Bugzilla?

IIRC there is no support for getConnection(username, password) in
Apache Commons DBCP pool at all, and it was a new feature in Tomcat
JDBC pool at that time.

Is it the problem, or something else is wrong?

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Robert Anderson
Sorry, for the wall of text. :)

"IIRC there is no support for getConnection(username, password) in
Apache Commons DBCP pool at all, and it was a new feature in Tomcat
JDBC pool at that time."

Yes,  it is the problem. I've lost many hours following an example in
documentation about ResourceLink and DataSource that does not work as
expected/described.


Best regards,

Robert

On Wed, May 23, 2012 at 8:26 PM, Konstantin Kolinko
<[hidden email]>wrote:

> 2012/5/24 Robert Anderson <[hidden email]>:
> >
> > 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.
> >
>
> Can you be more specific? You are just citing a wall of text and it is
> hard to digest.
>
> Can you open an issue in Bugzilla?
>
> IIRC there is no support for getConnection(username, password) in
> Apache Commons DBCP pool at all, and it was a new feature in Tomcat
> JDBC pool at that time.
>
> Is it the problem, or something else is wrong?
>
> Best regards,
> Konstantin Kolinko
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Robert Anderson
Hi,

Now it's working! Follows the script:

1) Tomcat 6.0.35: copy tomcat-jdbc.jar to CATALINA_HOME/lib. Tomcat 7.0.x
is ready.

2)  Create a global resource in CATALINA_HOME/conf/server.xml. Attributes
in bold *MUST *be present:

    <Resource name="jdbc/pgserver" auth="Container"
type="javax.sql.DataSource" removeAbandoned="true"
removeAbandonedTimeout="300"
                                   maxActive="200" maxIdle="10"
maxWait="10000"
                                   validationQuery="select 1"
                                   validationInterval="10000"
                                   testOnBorrow="true"
*
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"*
                                   driverClassName="org.postgresql.Driver"

url="jdbc:postgresql://localhost:5432/tjse"
                                   username="globaluser"
                                   password="globalpassword"
*                                   alternateUsernameAllowed="true"*

jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
                                   />


3)  Create a context.xml for apps;

The attribute "name" need not possess the same value of the attribute
"global".

--> CATALINA_HOME/conf/Catalina/localhost/app1.xml (shared connection with
different credential)

<Context>
    <ResourceLink name="jdbc/pgserver"
    global="jdbc/pgserver"
    type="javax.sql.DataSource"
*    factory="org.apache.naming.factory.DataSourceLinkFactory"*
*    username="userapp1"
    password="passwordapp1"*
    />
</Context>

--> CATALINA_HOME/conf/Catalina/localhost/app2.xml (shared connection with
default credential)

<Context>
    <ResourceLink name="jdbc/pgserver"
    global="jdbc/pgserver"
    type="javax.sql.DataSource"
    />
</Context>


4) JSP to test this feature (in both CATALINA_HOME/webapps/app1 and
CATALINA_HOME/webapps/app2):

<?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 = null;
    DataSource ds = null;
    Connection c = null;
    Statement stm = null;
    ResultSet rs = null;

    try {

    ctx = new InitialContext();
    ds = (DataSource)ctx.lookup("java:comp/env/jdbc/pgserver");
    c = ds.getConnection();
    stm = c.createStatement();

    rs = stm.executeQuery("select current_user");

    rs.next();

%>
    Current user: <%= rs.getString(1) %><br/>
<%
    } catch (Exception e) {
%>
    Error: <%= e.getMessage() %>
<%
    } finally {
     if (rs!=null) try  { rs.close(); } catch (Exception ignore){}
     if (stm!=null) try  { stm.close(); } catch (Exception ignore){}
     if (c!=null) try { c.close();} catch (Exception ignore){}    }
%>
</body>
</html>


5) App1 output:

Current user: userapp1

6) App2 output:

Current user: globaluser


Thanks and I hope this script can help others with the same problem.


Cheers,

Robert

On Wed, May 23, 2012 at 8:38 PM, Robert Anderson <[hidden email]> wrote:

> Sorry, for the wall of text. :)
>
>
> "IIRC there is no support for getConnection(username, password) in
> Apache Commons DBCP pool at all, and it was a new feature in Tomcat
> JDBC pool at that time."
>
> Yes,  it is the problem. I've lost many hours following an example in
> documentation about ResourceLink and DataSource that does not work as
> expected/described.
>
>
> Best regards,
>
> Robert
>
> On Wed, May 23, 2012 at 8:26 PM, Konstantin Kolinko <
> [hidden email]> wrote:
>
>> 2012/5/24 Robert Anderson <[hidden email]>:
>> >
>> > 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.
>> >
>>
>> Can you be more specific? You are just citing a wall of text and it is
>> hard to digest.
>>
>> Can you open an issue in Bugzilla?
>>
>> IIRC there is no support for getConnection(username, password) in
>> Apache Commons DBCP pool at all, and it was a new feature in Tomcat
>> JDBC pool at that time.
>>
>> Is it the problem, or something else is wrong?
>>
>> Best regards,
>> Konstantin Kolinko
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [hidden email]
>> For additional commands, e-mail: [hidden email]
>>
>>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Konstantin Kolinko
In reply to this post by Robert Anderson
2012/5/24 Robert Anderson <[hidden email]>:

> Sorry, for the wall of text. :)
>
> "IIRC there is no support for getConnection(username, password) in
> Apache Commons DBCP pool at all, and it was a new feature in Tomcat
> JDBC pool at that time."
>
> Yes,  it is the problem. I've lost many hours following an example in
> documentation about ResourceLink and DataSource that does not work as
> expected/described.
>

I added it to Bugzilla.
https://issues.apache.org/bugzilla/show_bug.cgi?id=53289

> Now it's working! Follows the script:
> 1) ...
> 2) ...
> 3) ...

Thank you. It is good to have a working example.

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Christopher Schultz-2
In reply to this post by Robert Anderson
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert,

On 5/24/12 7:50 AM, Robert Anderson wrote:
> Now it's working! Follows the script:

So, how does your "script" deviate from the Tomcat documentation? It
seems that you followed the docs and now it works. Right?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEUEARECAAYFAk++ScsACgkQ9CaO5/Lv0PAy4ACWNsbvFopO5tY0s0SXCDfnmXEb
7wCfTJA3lvlqkl7hXrAcVB70EREQ7EU=
=ssa2
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Robert Anderson
Chris,

Basically, the ResourceLink documentation doesn't say that to enable shared
pool with different credentials:

1) You have to add tomcat-jdbc.jar in Tomcat 6.0 classpath;

2) You have to put the attributes in global resource definition:
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" and
alternateUsernameAllowed="true".


Cheers,

Robert

On Thu, May 24, 2012 at 11:46 AM, Christopher Schultz <
[hidden email]> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Robert,
>
> On 5/24/12 7:50 AM, Robert Anderson wrote:
> > Now it's working! Follows the script:
>
> So, how does your "script" deviate from the Tomcat documentation? It
> seems that you followed the docs and now it works. Right?
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEUEARECAAYFAk++ScsACgkQ9CaO5/Lv0PAy4ACWNsbvFopO5tY0s0SXCDfnmXEb
> 7wCfTJA3lvlqkl7hXrAcVB70EREQ7EU=
> =ssa2
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Christopher Schultz-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Robert,

On 5/24/12 10:57 AM, Robert Anderson wrote:

> Chris,
>
> Basically, the ResourceLink documentation doesn't say that to
> enable shared pool with different credentials:
>
> 1) You have to add tomcat-jdbc.jar in Tomcat 6.0 classpath;
>
> 2) You have to put the attributes in global resource definition:
> factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" and
> alternateUsernameAllowed="true".

Gotcha: you didn't know that this was an example only relevant to
tomcat-jdbc (not that you should have -- I didn't know, either). I
thought you were already using tomcat-jdbc and I believe the docs
there are accurate.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk++ToIACgkQ9CaO5/Lv0PDFHQCgrXsXmL3C/Cc3a74Lt8ul8Ton
RyQAn0WwW4ZfQVJz3s8pHHh/ulBm7vwX
=XrXd
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Shared data source (Bug 49543)

Robert Anderson
Exactly, I had no way of knowing because the documentation of ResourceLink
does not inform these "details". :)

Konstantin was perfect in his description in bugzilla.



On Thu, May 24, 2012 at 12:06 PM, Christopher Schultz <
[hidden email]> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Robert,
>
> On 5/24/12 10:57 AM, Robert Anderson wrote:
> > Chris,
> >
> > Basically, the ResourceLink documentation doesn't say that to
> > enable shared pool with different credentials:
> >
> > 1) You have to add tomcat-jdbc.jar in Tomcat 6.0 classpath;
> >
> > 2) You have to put the attributes in global resource definition:
> > factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" and
> > alternateUsernameAllowed="true".
>
> Gotcha: you didn't know that this was an example only relevant to
> tomcat-jdbc (not that you should have -- I didn't know, either). I
> thought you were already using tomcat-jdbc and I believe the docs
> there are accurate.
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk++ToIACgkQ9CaO5/Lv0PDFHQCgrXsXmL3C/Cc3a74Lt8ul8Ton
> RyQAn0WwW4ZfQVJz3s8pHHh/ulBm7vwX
> =XrXd
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Loading...