Tomcat, Java & Oracle9. org.apache.naming.NamingContext lookup - java

I am using Tomcat and Java (through Eclipse) and Oracle Database 9.2.1
I am getting
org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference
java.sql.SQLException: oracle.jdbc.OracleDriver
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver
(PooledConnection.java:243)
My code is
context.xml
<Resource type="javax.sql.DataSource" auth="Container"
name="jdbc/charmDB"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#localhost:1521:db"
username="db" password="db" maxActive="20" maxIdle="10"
/>
<Resource name="jdbc/charmDB" auth="Container"
type="javax.sql.DataSource"
description="My Database"/>
Java code
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/charmDB");
Connection con = ds.getConnection();
Does anyone know what's wrong?
Thanks!

Your context.xml appears to have two resources with the same name: jdbc/charmDB.
Try resolving this issue first.
EDIT: Make sure your Oracle JDBC driver jar is in the Tomcat lib directory.

Related

I can't use JNDI in tomcat to connect MySQL

I did everything in this :
how to connect tomcat 7 and mysql
But it still isn't working..... why? What have I missed?
Enviroment
OS : Win7
Eclipse : J2EE Mars
Tomcat : tomcat 8.0
java : 1.8.0_73
db name : test
username: root
password: 123
controller:
writeDB testDB = new writeDB(tablename);
writeDB.java
....
Class.forName("com.mysql.jdbc.Driver");
try{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
//-------error-------
dataSource = (DataSource) envContext.lookup("jdbc/test");
//-------error-------
}catch(NamingException ex)
{
throw new RuntimeException(ex);
}
web.xml (in my project)
<!-- MySQL JNDI -->
<resource-ref>
<description>MySQL DB</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
context.xml
(in TOMCAT_HOME/conf)
(Also in workspace\Servers\Tomcat v8.0 Server at localhost-config)
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/eatST">
<Resource
name="jdbc/test"
auth="Container"
type="javax.sql.DataSource"
maxActice="100"
maxIdle="30"
maxWait="10000"
username="root"
password="123"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/test?
useUnicode=true&characterEncoding=UTF8"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
/>
</Context>
Logs
Servlet.service() for servlet [commentCtr] in context
with path [/eatST] threw exception
java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.BasicDataSource
cannot be cast to javax.activation.DataSource
at com.joe.db.writeDB.<init>(writeDB.java:58)
at com.joe.servlet.CommentCtr.doPost(CommentCtr.java:38)
at ............
You've imported (and presumable programmed to) an incorrect DataSource. Your exception (java.lang.ClassCastException ... cannot be cast to javax.activation.DataSource) is telling you that you have usedjavax.activation.DataSource, but you wanted javax.sql.DataSource. Modify com.joe.db.writeDB and change
import javax.activation.DataSource;
to
import javax.sql.DataSource;
Also, you don't need Class.forName("com.mysql.jdbc.Driver"); (it doesn't hurt anything, but JDBC drivers register themselves now).

java.sql.SQLException: Connection has already been closed

We are getting java.sql.SQLException: Connection has already been closed. exception intermittently while performing a transaction. We are using tomcat 7.X and below is the configuration.
<Context docBase="ROOT" reloadable="true" antiJARLocking="true">
<Resource
name="jdbc/DS"
auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://XXXXXXX"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
username="XXXXXX"
password="XXXXXX"
maxActive="20"
maxIdle="3"
minIdle="3"
maxWait="10000"
removeAbandoned="true"/>
</Context>
Probably we are missing some configuration or property here that is causing the issue.
Please suggest a way fix this issue or help to find out the root cause.
Following configuration worked for me
<Context context="ROOT" debug="0" reloadable="false" useHttpOnly="true" cacheMaxSize="40960" cacheTTL="60000" cachingAllowed="true" antiJARLocking="true">
<Resource name="XYZ" auth="Container"
description="Exchange DB Connection"
dataSourceClassName="org.postgresql.ds.PGSimpleDataSource"
dataSource.serverName="XXXXX"
dataSource.databaseName="XXXX"
dataSource.portNumber="XXXX"
dataSource.user="xyz"
dataSource.password="xyz"
maximumPoolSize="20"
minimumIdle="5"
connectionTimeout="300000"
factory="com.zaxxer.hikari.HikariJNDIFactory"
registerMbeans="true"
type="javax.sql.DataSource" />
The key value here is connectionTimeout.
The factory which you are currently using has a default timeout, after that it forces session to close.
The connection timeout value above worked for me , for your application scenarios you'll have to experiment a bit to get the right value.
add below value:
removeAbandonedTimeout="600"

JDBC connection pooling with Struts 2

I am using connection pooling in Struts2 application but after some request, I am not getting any result even though I am closing the connection in our application. Please give me proper solution.
Context.xml
<Resource name="jdbc/abbas"
auth="Container"
type="javax.sql.DataSource"
username="myuser"
password="pass#123"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.14.65/abbas"
maxActive="15"
maxIdle="3"/>

Spring / Tomcat - Error in DataSource (NameNotFoundException)

I am trying to connect a Spring MVC 4 application to MY Sql local BBDD.
This is the files:
Spring MVCConfiguration File:
#Bean
public DataSource dataSource() throws Exception {
Context cts = new InitialContext();
DataSource dts = (DataSource) cts.lookup("java:/comp/env/jdbc/etielaBBDD");
return dts;
}
Tomcat Context.xml:
<ResourceLink name="jdbc/etielaBBDD"
global="jdbc/BBDD"
auth="Container"
type="javax.sql.DataSource" />
Tomcat Server XML:
<Resource name="jdbc/BBDD" global="jdbc/BBDD" auth="Container" type="javax.sql.DataSource"
username="XXXXXX"
password="XXXXXX"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/confluenceuseUnicode=true&characterEncoding=utf8"
maxActive="15"
maxIdle="7"
defaultTransactionIsolation="READ_COMMITTED"
validationQuery="Select 1" />
And, when I Start Tomcat, this error appears:
javax.naming.NameNotFoundException: Name jdbc/BBDD is not bound in this context
I am two days trying to solve this error with no solution. Any idea? Thanks.

TOMCAT 6 Problems with DataSourceFactory and SQLServerDriver

I'm working with TOMCAT6.
In the context.xml I changed the connection pool to tomcat jdbc connection pool. I dropped the dependent jars in the lib, added the factory to the context and all seems to work just fine until TOMCAT tries to lookup MS SQLServerDriver.
org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference java.sql.SQLException: com.microsoft.sqlserver.jdbc.SQLServerDriver
I do not have this problem when I use the default Commons DBCP implementation (simply by removing the factory attribute).
Here is the resource definition within the Context.xml configuration
<Resource
RTentry="True" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
username="xxxx"
password="xxxx"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://MSSQL02\LIFETIME:49658;databaseName=MSSQL02_LT23"
maxActive="8"
maxIdle="4"
name="jdbc/LifetimeDB" />
Does anyone has a clue how this can happen?

Categories

Resources