Using JNDI for remote Database

R

Rahul

Hi all,

I am using tomcat JNDI for getting databse connection in my application

For this I have created a context.xml file in myAPP/META-INF
which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/myAPP" docBase="myAPP" debug="5" reloadable="true"
privileged="true" crossContext="true">
<Resource name="jdbc/myAPP" auth="Container"
type="javax.sql.DataSource" maxActive="30" maxIdle="10"
maxWait="6000"
username="user" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDB" removeAbandoned="true"
autoReconnect="true"
validationQuery="select now()"
factory="org.apache.commons.dbcp.BasicDataSourceFactory"/>
</Context>


This works absolutely fine. But when I try to connect to a remote
database by changing url in above
file to "jdbc:mysql://192.168.5.65:3306/myDB" my application fails
It creates following exception while retrieving a connection:
org.apache.tomcat.dbcp.dbcp.SQLNestedException:
Cannot create PoolableConnectionFactory (Unknown database 'myDB')


code I have written for fetching connection is:


Context ctx = (Context) new InitialContext()
.lookup("java:comp/env");
if (ctx == null)
{
throw new Exception("No context available");
} else
{
dataSource = (DataSource) ctx.lookup("jdbc/icontact");
Connection connection = dataSource.getConnection();
}


can anybody what else I need to configure for getting databse
connections from remote machine.

thanks in advance
 
A

Andreas Thiele

Rahul said:
...
I am using tomcat JNDI for getting databse connection in my application
...
This works absolutely fine. But when I try to connect to a remote
database by changing url in above
file to "jdbc:mysql://192.168.5.65:3306/myDB" my application fails
It creates following exception while retrieving a connection:
org.apache.tomcat.dbcp.dbcp.SQLNestedException:
Cannot create PoolableConnectionFactory (Unknown database 'myDB')
...

To me this seems to be a misconfiguration of the remote database. Are you
sure you have 'myDB' on the remote machine?

Changing the IP address as in your example should work, normally.


Andreas
 
R

Roedy Green

To me this seems to be a misconfiguration of the remote database. Are you
sure you have 'myDB' on the remote machine?

Often "can't find" problems are actually spelling errors. In Java,
presume everything is case sensitive and you won't get in trouble.
 
R

Rahul

I am absolutely sure about that. since I am using same script for
creating database on local machine as well as on remote machine
 
R

Rahul

Finally I figured out the problem

Actually the context file which tomcat creates for every application
in conf/<engine>/<application> folder was not getting updated.
It was having the localhost settings instead of remote server.

After correcting this file my application is working perfectly.

Thanks for the help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top