Tomcat GlobalNamingResources ResourceLink Oracle get DataSource - oracle

I'm having difficulty getting a JDBC DataSource using Tomcat 7
javax.naming.NameNotFoundException: Name [jdbc/weblogin01b] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.factory.ResourceLinkFactory.getObjectInstance(ResourceLinkFactory.java:92)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.apache.naming.NamingContext.lookup(NamingContext.java:841)
at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
In CATALINA_BASE/conf/server.xml
I have included a variety of slightly different resources
all aiming to be the same connection hopefully one of them will be right:
<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"
username="weblogin01"
password="xxxxx"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.pool.OracleDataSource"
description="Global Address Database"
url="jdbc:oracle:thin:#10.15.120.29:1522:DGSPC"
maxActive="15"
maxIdle="3" />
<Resource name="jdbc/weblogin01b"
user="weblogin01"
password="xxxxx"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:#10.15.120.29:1522:DGSPC"
maxActive="20"
maxIdle="3"
maxWait="-1" />
<Resource name="jdbc/weblogin01c"
user="weblogin01"
password="xxxxx"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#10.15.120.29:1522:DGSPC"
maxActive="20"
maxIdle="3"
maxWait="-1" />
</GlobalNamingResources>
In META-INF/context.xml
I have a ResourceLink to each resource
<Context antiJARLocking="true" path="/testDbAccess">
<ResourceLink name="jdbc/weblogin01"
global="jdbc/weblogin01"
type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/weblogin01b"
global="jdbc/weblogin01b"
type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/weblogin01c"
global="jdbc/weblogin01c"
type="javax.sql.DataSource"/>
</Context>
In web.xml I made no changes related to JDBC on the understanding that
the ResourceLink elements will be sufficient.
In Java code I try to get a DataSource as follows:
String dbUser = "weblogin01b";
try {
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
ds = (DataSource) envCtx.lookup("jdbc/" + dbUser);
if (ds == null) {
logger.log(Level.WARNING,"Null datasource for " + dbUser);
}
}
Given the exception above, it never gets a DataSource, this is the same for:
jdbc/weblogin01
jdbc/weblogin01b
jdbc/weblogin01c
I'm finding Tomcat an uphill struggle, any help would be much appreciated.

Part 1 of the puzzle is solved:
I was editing the wrong copy of server.xml
I am running Tomcat under NetBeans which makes a separate copy of CATALINA_HOME which it configures as CATALINA_BASE, I had not noticed the separate copy.
I wish Tomcat's CATALINA_HOME and CATALINA_BASE were always separate and distinctly different, i.e. no duplication of files.
Part 2 is a different story:
java.sql.SQLException: ORA-01017: invalid username/password; logon denied
I know that I've specified the correct username and password but I'm not seeing these values logged anywhere, I'm guessing that I've specified the correct values in the wrong properties.

Part 2 of the puzzle is solved:
That should be:
username="weblogin01"
NOT
user="weblogin01"

Related

How to get oracle connection from hibernate connection

I am trying to get oracle connection from hibernate. below is the code that I am using.
server.xml
<library id="jdbcDriverFiles">
<fileset dir="libraries" includes="ojdbc7.jar" />
</library>
<dataSource id="x" jndiName="jndisce"
statementCacheSize="20" type="javax.sql.DataSource">
<containerAuthData password="Pwd" user="User" />
<jdbcDriver javax.sql.DataSource="oracle.jdbc.pool.OracleDataSource" libraryRef="jdbcDriverFiles" />
<connectionManager agedTimeout="600"
connectionTimeout="180" maxIdleTime="1800" maxPoolSize="70"
minPoolSize="1" purgePolicy="EntirePool" reapTime="180" />
<properties.oracle
URL="url"
password="Pwd" portNumber="123" serverName="server"
user="User" />
</dataSource>
<webApplication id="cm" location="cm.war"
name="cm">
<classloader commonLibraryRef="jdbcDriverFiles" />
</webApplication>
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jndisce");
Connection conn = ds.getConnection();
OracleConnection con = conn.unwrap(OracleConnection.class);
and below is the error that I am getting.
java.sql.SQLException: DSRA9122E: com.ibm.ws.rsadapter.jdbc.v41.WSJdbc41Connection#7acaddcd does not
wrap any objects of type oracle.jdbc.OracleConnection.
This has nothing to do with Hibernate, you are not even using a Hibernate API here. You are trying to unwrap an OracleConnection from a IBM datasource connection but apparently it does not let you do this. According to the following article, this might be due to class loader issues: how to unwrap PostgreSQL connection from the IBM WSJdbc41Connection

Name is not bound in this Context... Datasource not found

Am building a small Jersey (1.9) REST Service and having a Java class as sub-resource where I connect to local database (Postgres 9.3).
For the datasource I have already add the entries in Context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/userProfile">
<Resource
auth="Container"
driverClassName="org.postgresql.Driver"
maxActive="100"
maxIdle="30"
maxWait="10000"
name="jdbc/apiUserProfile"
password="postgres"
type="javax.sql.DataSource"
url="jdbc:postgresql://localhost:5432/apiUserProfile"
username="postgres"/>
</Context>
When I run the application and call the following resource:
http://localhost:8084/userProfile/rest/user/conn
the page is blank - no content- and the tomcat (8.0) on netbeans (8.1) is throwing Error: Null Pointer Exception
javax.naming.NameNotFoundException: Name [jdbc/apiUserProfile] is not bound in this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:157)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at net.rest.dao.DbConn.apiUserProfileConn(DbConn.java:23)
at net.rest.service.userProfile.returnDatabaseStatus(userProfile.java:51)
I also already have the JAR files in the librairies:
lib/mysql-connector-java-5.1.39-bin.jar
lib/postgresql-9.3-1100-jdbc4.jar
and here is the sub-resource class for the datasource connection:
package net.rest.dao;
import javax.naming.*;
import javax.sql.*;
public class DbConn {
private static DataSource DbConn = null;
private static Context context = null;
public static DataSource apiUserProfileConn() throws Exception {
if(DbConn != null){
return DbConn;
}
try {
if(context == null){
context = new InitialContext();
}
DbConn = (DataSource) context.lookup("jdbc/apiUserProfile");
}
catch (Exception e) {
e.printStackTrace();
}
return DbConn;
}
}
Any Idea pls. how to fix this..
Many Thanks
a.kasbi
The issue is resolved now.. the Apache Tomcat Doc was very helpful:
http://localhost:8080/docs/jndi-datasource-examples-howto.html
http://localhost:8080/docs/jndi-datasource-examples-howto.html#PostgreSQL
The solution for me was adding the following into context.xml under: META-INF
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/apiRest">
<Resource name="jdbc/apiUserProfile" auth="Container"
type="javax.sql.DataSource" driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/apiUserProfile"
username="postgres" password="postgres" maxTotal="20" maxIdle="10"
maxWaitMillis="-1"/>
</Context>
and the following in web.xml:
<resource-ref>
<description>postgreSQL Datasource example</description>
<res-ref-name>jdbc/apiUserProfile</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
The lookup argument in the java Class for the Datasource connection:
...
DbConn = (DataSource) context.lookup("java:/comp/env/jdbc/apiUserProfile");
...
Thanks

Setting up JNDI Datasource with oracle in Tomcat

I can't make it work to get connection from DataSource obtained from JNDI in tomcat 7.0, resulting in error like this:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of cla
ss '' for connect URL 'null'
I have done in many ways following the doc and other people suggestions, but still not able to fix it. Here are my settings:
In web.xml:
<resource-ref>
<description>MyDataSource</description>
<res-ref-name>jdbc/MyDataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
In context.xml:
<Context>
<Resource name="jdbc/MyDataSource" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="abc" password="abc" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:#123.123.123.123:1521:xe"/>
<!-- // tried both driver class names but all not work driverClassName="oracle.jdbc.driver.OracleDriver" -->
</Context>
Also, I tried to put that in server.xml, also not work
<Resource name="jdbc/MyDataSource" auth="Container" type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:#123.123.123.123:1521:xe"
servicename="MyDataSource" username="abc" password="abc"
maxActive="20" maxIdle="10" maxWait="20000"/>
<ResourceParams name="jdbc/MyDataSource">
<parameter>
<name>user</name>
<value>abc</value>
</parameter>
<parameter>
<name>password</name>
<value>abc</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
</ResourceParams>
Put classes12.jar as well as ojdbc14.jar in Tomcat 7.0\lib
Can anyone point me the right way?
I wasted so much time on that configuration but still cannot make it work. Thanks in advance!
John
Stack Trace as below:
DS: org.apache.tomcat.dbcp.dbcp.BasicDataSource#497062
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at DBTest.doGet(DBTest.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:298)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at sun.jdbc.odbc.JdbcOdbcDriver.getProtocol(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.knownURL(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcDriver.acceptsURL(Unknown Source)
at java.sql.DriverManager.getDriver(Unknown Source)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 21 more
And here is the code, exception at ds.getConnection. (It is not typical way to get DS, bcoz need to match WebSphere way...)
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
initCtx = new InitialContext(env);
DataSource ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/MyDataSource");
System.err.println("DS: " + ds.toString());
Connection conn = ds.getConnection();
I realized that I defined the resource twice, once in my TOMCAT_HOME/conf/context.xml and in my applicationPath/META-INF/context.xml. Once I removed the resource from TOMCAT_HOME/conf/context.xml everything worked great.

java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource

I'm running Tomcat 7.0.22 and I wrote a simple servlet that connects to a SQL Anywhere 12.0 database. When I run the servlet I get java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource. My ./META-INF/content.xml file looks like the following:
<Context>
<Resource name="jdbc/FUDB"
auth="Container"
type="javax.sql.DataSource"
username="dba"
password="sql"
driverClassName="sybase.jdbc.sqlanywhere.IDriver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
url="jdbc:sqlanywhere:uid=dba;pwd=sql;eng=BTH476331A_FedUtilization;"
accessToUnderlyingConnectionAllowed="true"
maxActive="8"
maxIdle="4" />
My webapp web.xml looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<display-name>FedUtilization</display-name>
<servlet>
<servlet-name>Report1</servlet-name>
<display-name>Report1</display-name>
<servlet-class>com.sapgss.ps.servlet.Report1</servlet-class>
Report1
/Report1
SQL Anywhere 12.0.1 server jdbc3
jdbc/FUDB
javax.sql.DataSource
Container
The servlet code is as follows:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import org.apache.catalina.core.StandardContext.*;
import org.apache.tomcat.jdbc.pool.*;
import com.sapgss.ps.dbutil.*;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
public class Report1 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("Hello Elaine!");
out.println("");
out.println("");
out.println("Hello Elaine! ");
// This is how to code access to the database in Java Context
initCtx = new InitialContext(); Context envCtx = (Context)
initCtx.lookup("java:comp/env"); DataSource ds = (DataSource)
envCtx.lookup("jdbc/FUDB");
Connection conn = ds.getConnection(); .
.
.
}
}
The error happens when I try to get a DataSource at this line:
DataSource ds = (DataSource) envCtx.lookup("jdbc/FUDB");
Thanks in advance I'm pulling my hair out.
In my case I just forgot to put:
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
in my /tomcat7/conf/context.xml. Just added and all worked fine.
My context.xml:
<Context>
<Resource name="jdbc/gestrel" auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/g...."
username="postgres"
password="....." maxActive="20" maxIdle="10"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
maxWait="-1"/>
</Context>
Today I've spent a half of the day trying to deal with the similar issue. I have tomcat server.xml file defining context like this:
<Context docBase="app" path="/my_context_path">
</Context>
Then I tried to add jdbc pool support using org.apache.tomcat.jdbc.pool.DataSource.
Just added resource definition to my server.xml context definition (see above). And of cause I defined resource-ref in my web.xml.
But there was always org.apache.tomcat.dbcp.dbcp.BasicDataSource returned. I spent time debugging tomcat and finally got to the following:
If I define resource in server.xml context - tomcat does NOT pick that up.
If define in web archive's META-INF/context.xml works ok.
If define in server.xml GlobalNamingResources tag - tomcat does NOT pick that up.
If define in tomcat global context.xml file works ok.
If you specify resource-ref in web.xml for bad cases 1,3 - tomcat will return org.apache.tomcat.dbcp.dbcp.BasicDataSource, cause as I can see it is some kind of default. BUT using such data source returned will cause something like this:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
If not specify resource-ref in web.xml then you will get an exception telling that resource with such name could not be find.
Also I noticed that for good cases 2,4 specifying resource-ref in web.xml is not necessary (works with and without resource-ref).
Try some of the cases I described. I hope something will help.
I would try to define resource in tomcat global context.xml file.
Good luck!
P.s. I run 7.0.22 version as well.
The solution is to import javax.sql.DataSource in your servlet as you define the resouce in context.xml of type="javax.sql.DataSource"

Configure DB Connection Pooling - Axis2 webservice

I'd like to know how to modify the server.xml file so all my webservices built on axis2 can talk to the DB using Connection Pooling. Each webservice has a different data source, one points to one instance of the DB and the other to another DB server. How do I specify the context that should be used by each service?
Thanks in advance,
Pojo
If you want to use connection pool in your project , Ensure that you have the following code set up for the Tomcat connection pooling to work in context.xml file:
1)Create file with name "context.xml" if it's not exist under directory "WebContent/META-INF/context.xml" with the following content:
For My Project , Please modify it with appropriate value :
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/dbcp" docBase="dbcp">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
removeAbandoned="true" removeAbandonedTimeout="30" maxActive="80"
maxIdle="30" maxWait="10000" username="sontn" password="nhantien"
driverClassName="org.postgresql.Driver"
url = "jdbc:postgresql://localhost/group8" useUnicode="true"
characterEncoding="utf-8" characterSetResults="utf8"/>
</Context>
Or you can copy file : context.xml into directory "$Catalian\webapps\axis2\META-INF"
How can you get connection pool?
In your webservice method : create method getConnection() with following content:
public Connection getConnection() {
Connection connection = null;
try {
Context envCtx = (Context) new InitialContext().lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
connection = ds.getConnection();
}
catch (Exception e) {
System.out.println("Connection error: " + e.getMessage());
}
return connection;
}
Thanks

Resources