setting jdbc user/pwd at run time?
843838Dec 5 2005 — edited Mar 3 2006Hi all
I was wondering if you could help me out and send me an example for the web.xml, context.xml and what needs to be in my WEB-INF/lib directory and .jsp in order to deploy my WAR file so that db credential be provided at runtime. I don't want to statically write them anywhere because:
A. my data source needs to be accessed by various users
B. it's silly+unsecure :-)
I understand it has something to do with specifying 'res-auth' as 'Application' instead of 'Container', but I couldn't get this to work...
What I've already done on tomcat 5.5.7:
Set up a JNDI entry called "jdbc/myDSN" in Tomcat Admin:
URL: jdbc:microsoft:sqlserver://myhost
Driver: com.microsoft.jdbc.sqlserver.SQLServerDriver
User: test (a dummy user, doesn't really exist)
no password
I copied the 3 .jars for sql server JDBC drivers to my app's WEB-INF/lib
I setup the following .xml files for my app:
(I took this from some Tomcat JNDI website)
context.xml:
<Context path="/myapp">
<ResourceLink name="jdbc/myDSN" type="javax.sql.DataSource" auth="Application" global="jdbc/myDSN"/>
</Context>
part of my web.xml :
<resource-ref>
<description>Tomcat DBCP</description>
<res-ref-name>jdbc/myDSN</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Application</res-auth>
</resource-ref>
This is the test code:
InitialContext ictx = new InitialContext();
DataSource ds = ictx.lookup("java:comp/env/jdbc/myDSN");
java.sql.Connection con = ds.getConnection("myuser", "mypwd"); <-- exception:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory ([Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Login failed for user 'test'.)
I tried moving the resource under my app's context without specifying user/pwd:
<Resource name="jdbc/eAudit_DSN"
auth="Application"
type="javax.sql.DataSource"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
url="jdbc:microsoft:sqlserver://770230audit1"
/>
this time I got:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory ([Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection
But my SQL server IS configured for SQL authentication!
If I add the 'username' attribute to the resource:
username='aaa'
the exception changes to:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory ([Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Login failed for user 'aaa'.
So it looks as if everyone ignores the user/pwd arguments I send to my getConnection method :-(((
Any ideas?
Thanks...