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