JNDI DataSource configuration in Tomcat 7 - java

I am new to JNDI and I am trying to get my db connection working. So far no luck.
I either get a message stating: "Name [java:comp/env] is not bound in this Context. Unable to find [java:comp]"
or I received a time out.
Here's information about my current configuration.
Tomcat: Apache Tomcat/7.0.29
JMV: 1.7.0_06-b24
OS: Win 10 Pro
Tomcat\conf\web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/myDatabaseName</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Tomcat\conf\context.xml
<ResourceLink type="javax.sql.DataSource"
name="jdbc/localRemarket"
global="jdbc/remarket"
/>
I also tried to put the resource in context.xml to make sure it's findable:
<Resource
type="javax.sql.DataSource"
name="jdbc/myDatabaseName"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDatabaseName"
username="myUsername"
password="myPassword"
maxActive="1500"
maxIdle="200"
maxwait="-1"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="true"
validationQuery="SELECT 1"
timeBetweenEvictionRunsMillis="2000"
minEvictableIdleTimeMillis="15000"
removeAbandoned="true"
removeAbandonedTimeout="5"
/>
Tomcat\conf\server.xml
<Resource
type="javax.sql.DataSource"
name="jdbc/myDatabaseName"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDatabaseName"
username="myUsername"
password="myPassword"
maxActive="1500"
maxIdle="200"
maxwait="-1"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="true"
validationQuery="SELECT 1"
timeBetweenEvictionRunsMillis="2000"
minEvictableIdleTimeMillis="15000"
removeAbandoned="true"
removeAbandonedTimeout="5"
/>
java code:
Connection conn;
public void openMyConnection() {
try {
Properties props = new Properties();
props.put("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory");
InitialContext ctx = new InitialContext(props);
Context envCtx = (Context) ctx.lookup("java:comp/env"); // <<<<< PRB HERE
// error message : Name [java:comp/env] is not bound in this Context. Unable to find [java:comp]
org.apache.tomcat.jdbc.pool.DataSource ds = (org.apache.tomcat.jdbc.pool.DataSource) envCtx.lookup("jdbc/localDB");
conn = ds.getConnection();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
if I change
props.put("java.naming.factory.initial", "org.apache.naming.java.javaURLContextFactory");
for
props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
I get :
Receive timed out
I have reviewed many posts related to JNDI including the following two that were the most helpful:
http://tomcat.apache.org/tomcat-7.0-doc/jndi-datasource-examples-howto.html
and
https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-datasource-jndi-example/
Please note that I read the How to configure jndi DataSource in Tomcat 7 but it doesn't provide a solution for my problem.
Can anyone please help resolve this issue?

It worked for me, when I configured the datasource directly in the webapp (file META-INF/context.xml):
<Context >
<Resource name="jdbc/EmployeeDB"
auth="Container"
type="javax.sql.DataSource"
username="scott"
password="tiger"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#127.0.0.1:1521:mysid"
maxActive="8"
maxIdle="4"/>
</Context>

Related

Not able to connect to XA datasource using JNDI

I am trying to connect my java spring batch application to datasource using JNDI. My application requires XA datasource. I tried to configure normal datasource using below code and it seems to connect to it perfectly. But when i try to change the driver class to XA(oracle.jdbc.xa.client.OracleXADataSource) it doesn't seem to connect.i tried changing the type to javax.sql.XADataSource. Can someone tell me if i need to set additional properties for XA connectivity ?
<Resource name="jdbc/DS-ref"
auth="Container"
type="javax.sql.DataSource"
username="user"
password="password"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#xxx"
initialSize="20"
maxWaitMillis="15000"
maxTotal="75"
maxIdle="20"
testOnBorrow="true"
validationQuery="select 1 from dual" />
If i change the code in context.xml, I get error saying " Name [jdbc/DS-ref] is not bound in this Context. Unable to find [jdbc]."
Context.xml
<Resource name="jdbc/DS-ref"
auth="Container"
type="javax.sql.XADataSource"
username="user"
password="password"
driverClassName="oracle.jdbc.xa.client.OracleXADataSource"
url="jdbc:oracle:thin:#xxx"
initialSize="20"
maxWaitMillis="15000"
maxTotal="75"
maxIdle="20"
testOnBorrow="true"
validationQuery="select 1 from dual" />
web.xml
<resource-ref id="ResourceRef_CrcDataSource">
<description></description>
<res-ref-name>jdbc/DS-ref</res-ref-name>
<res-type>javax.sql.XADataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
datasource.java
// Use JNDI via Spring
Object tmpDs;
setResourceRef(true); // Make "java:comp/env/" prefix optional
try {
log.debug("pJndiName" +pJndiName);
tmpDs = lookup(pJndiName, DataSource.class);
log.debug("tmpDs"+tmpDs);
}
catch (Exception e) {
throw new RuntimeException("Error looking up DataSource via JNDI. jndiName: " + pJndiName, e);
}
if (null == tmpDs) {
throw new RuntimeException("DataSource JNDI lookup returned null. jndiName: " + pJndiName);
}
if (!DataSource.class.isInstance(tmpDs)) {
throw new RuntimeException("Illegal class returned by DataSource JNDI lookup. " + "jndiName: "
+ pJndiName + ", returned class: " + tmpDs.getClass().getName());
}
return (DataSource) tmpDs;

Problems with Tomcat8 using connection pooling to OracleDB

We have an application provided by a 3rd party vendor that runs on Tomcat 8 and JDK 8 to an Oracle 12 DB with ojdbc7.jar & xdb6.jar driver. The application works, but is slower than expected. When investigating it appears the application is configured to use connection pooling, but it appears the application is creating new connections per query, and not using any of the initially created connections to the Database.
Unfortunately, I don't have access to the code of the 3rd party app. But, hoping for an idea on what I am missing in the Tomcat setting to have pooling work.
I've tried going through Apache's documentation for the older Oracle connections, and trying other options found on the web.
<Resource name="jdbc/DataSource" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#localhost:1521:XE"
username="myProxyUser" password="myPassword"
initialSize="5" maxTotal="100" maxIdle="-1"
maxWaitMillis="30000"
validationQuery="select 1 from dual"
testOnBorrow="true"
accessToUnderlyingConnectionAllowed = "true"
connectionProperties="defaultRowPrefetch=100"
removeAbandoned = "true"
removeAbandonedTimeout = "30"/>
You can check tomcat docs, mainly use factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
example on how to configure a resource for JNDI lookups
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="100"
minIdle="10"
maxWait="10000"
initialSize="10"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="root"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysql"/>
you can specify pooling by defining type and factory
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"

Naming error while using multiple connection pools in a Tomcat 8 application

I have an application that uses the built-in Tomcat connection pool, and for the most part it works. A problem arises when I'm trying to use another pool to get a different set of connection from the same database, but from a different username/password (It's an oracle database that uses 2 usernames to access different namespaces of tables and function).
The first pool is accepted, but for the second one, I'm getting this error
15:09:47.157 [http-nio-8081-exec-5] ERROR com.applicationname.providers.ConnectionManager - NamingException in MyDataSource
javax.naming.NameNotFoundException: Name [appdb_two] is not bound in this Context. Unable to find [appdb_two].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818) ~[catalina.jar:8.0.15]
Here is my configuration:
server.xml
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users-->
<Resource name="UserDatabase"
auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/appdb_two"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"/>
<Resource name="jdbc/appdb_one"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"/>
</GlobalNamingResources>
context.xml
<ResourceLink name="jdbc/appdb_two"
auth="Container"
username="DBONE"
password="xxxx"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:#xx.xxx.xxx.xxx:1521:XE"
initialSize="20"
maxActive="50"
maxIdle="20"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"/>
<ResourceLink name="jdbc/appdb_one"
auth="Container"
username="DBTWO"
password="xxxx"
type="javax.sql.DataSource"
url="jdbc:oracle:thin:#xx.xxx.xxx.xxx:1521:XE"
initialSize="20"
maxActive="50"
maxIdle="20"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"/>
...
I think you're looking up the second datasource via name "appdb_two", but should be using "jdbc/appdb_two" - it's hard to see from the stacktrace alone, code for lookup would be helpful.
Also check that your web.xml has references to both data sources (<resource-ref> elements).

Cannot create JDBC driver of class '' for connect URL 'null' :dbcp.SQLNestedException

I am unable to get database connection in Tomcat7. i am using oracle linux as a OS. Below are my DB connection pool configuration.
server.xml
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource name="jdbc/weblogin01"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="${resource.weblogin01.url}"
username="${resource.weblogin01.username}"
password="${resource.weblogin01.password}"
initialSize="2"
maxActive="20"
maxIdle="10"
minIdle="2"
maxWait="-1"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="20000"
validationQuery="select * from dual" />
<Resource name="jdbc/osswebportal"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="${resource.osswebportal.url}"
username="${resource.osswebportal.username}"
password="${resource.osswebportal.password}"
initialSize="1"
maxActive="20"
maxIdle="10"
maxWait="-1"
testWhileIdle="true"
timeBetweenEvictionRunsMillis="60000"
validationQuery="select * from dual" />
web.xml
<resource-ref>
> <res-ref-name>jdbc/osswebportal</res-ref-name>
> <res-type>javax.sql.DataSource</res-type>
> <res-auth>Container</res-auth>
> <res-sharing-scope>Shareable</res-sharing-scope>
> </resource-ref>
> <resource-ref>
> <res-ref-name>jdbc/weblogin01</res-ref-name>
> <res-type>javax.sql.DataSource</res-type>
> <res-auth>Container</res-auth>
> <res-sharing-scope>Shareable</res-sharing-scope>
> </resource-ref>
context.xml
<ResourceLink global="jdbc/weblogin01" name="jdbc/weblogin01" type="javax.sql.DataSource"/>
<ResourceLink global="jdbc/osswebportal" name="jdbc/osswebportal" type="javax.sql.DataSource"/>
i have also define all the db details in tomcat's catalina.properties
resource.osswebportal.url=jdbc:oracle:thin:#test.com:1522:GPSP
resource.osswebportal.username=User
resource.osswebportal.password=password
resource.weblogin01.url=jdbc:oracle:thin:#test2:1522:GSPS
resource.weblogin01.username=User1
resource.weblogin01.password=Password
And also i have placed the jdbc jar in both tomcat's lib directory as well Applications's WEB_INF/lib directory.
put the Driver Jar file in the server lib folder, I think this will solve your issue.

NoInitialContextException

I have a problem.
I want to connect to database using JDBC, I have Tomcat server. For this I use connection pool.
According to Internet tutorials I've written:
context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/Server" docBase="dbcp" debug="5"
reloadable="true" crossContext="true">
<Resource name="jdbc/TestDB" auth="Container"
type="javax.sql.DataSource" removeAbandoned="true"
removeAbandonedTimeout="30" maxActive="100"
maxIdle="30" maxWait="10000" username="root"
password="newpass"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/delta_server"/>
</Context>
web.xml :
<resource-ref>
<description>DB Connection Pooling</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
and I try to connect...
Connection conn=null;
DataSource ds;
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
ds = (DataSource) envContext.lookup("jdbc/TestDB");
conn = ds.getConnection();
But I get a mistake:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
What to do???

Categories