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

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

Related

Tomcat servlet configure JNDI

Good day, respective all!
My environment:
Tomcat 8.5 under windows 64-bit
All needed jars are placed into $CATALINA_HOME/lib
It is my first attempt to write servlet using ConnectionPool.
After "googling"I made an entry inside $CATALINA_HOME/conf/server.xml:
<context docbase="msgsend" path="/msgsend" reloadable="true">
<context docbase="ssr" path="/ssr" reloadable="true">
<Resource
name="jdbc/OrServlet"
auth="Container"
type="javax.sql.DataSource"
user="STERN"
username="STERN"
password="pwdxxx"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:STERN/pwdxxx#XEPDB1"
/>
</context>
I included following into WEb-INF/web.xml:
<resource-ref>
<description>just a test</description>
<res-ref-name>jdbc/OrServlet</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
When I try to connect to Oracle in traditional way:
try {
cn = DriverManager.getConnection(
"jdbc:oracle:thin:#XEPDB1", "STERN", "pwdxx");
}
catch (Exception e) { }
everything works fine.
But when I try to connect through context:
Connection cn = null;
DataSource ds = null;
try
{
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
ds = (DataSource)envCtx.lookup("jdbc/OrServlet");
cn = ds.getConnection();
}
catch(NamingException n) {}
catch(SQLException s) {out.println(ds.getClass().getName()+" exception "+s);
;}
I get :
**exception java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'**
and ds.getClass().getName() in exception handler returns:
org.apache.tomcat.dbcp.dbcp2.BasicDataSource
which is distinct from javax.sql.DataSource as declared in
and .
And it seems to me that ds = (DataSource)envCtx.lookup("jdbc/OrServlet")
doesn't wvwn try to look inside Resource section.
What I missed?
Any help will be very appreciated.
Regards,
Andrew.

Cannot connect to Oracle DB through Java Context method. \Error: Name [comp/env] is not bound in this Context. Unable to find [comp]

I am learning Apache 9.0 web server and Oracle 11g DB within Eclipse EE environment.
When I simply connect using
conn = DriverManager.getConnection(url, "name", "pw");
I can connect to Oracle DB and send SQL and receive result. (everything works.)
But when I try to connect with this method below, I get error.
public class JdbcUtil {
public static Connection getConnection() {
Connection con = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/OracleDB");
System.out.println("ds = " + ds);
con = ds.getConnection();
System.out.println("con = " + con);
con.setAutoCommit(false);
System.out.println("DB connect success! Util");
} catch (Exception e) {
System.out.println("DB connect failure! Util");
e.printStackTrace();
}
return con;
}
Error message is
javax.naming.NameNotFoundException: Name [comp/env] is not bound in this Context. Unable to find [comp].
at org.apache.naming.NamingContext.lookup(NamingContext.java:833)
at org.apache.naming.NamingContext.lookup(NamingContext.java:174)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at db.JdbcUtil.getConnection(JdbcUtil.java:14)
at service.mListService.memberList(mListService.java:15)
at controller.mListController.doProcess(mListController.java:45)
So the error is at Context envCtx = (Context) initCtx.lookup("java:comp/env");
But sometimes error is at DataSource ds = (DataSource) envCtx.lookup("jdbc/OracleDB");
This is my context.xml in META-INF (of my current project)
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name = "jdbc/OracleDB"
auth = "Container"
type = "javax.sql.DataSource"
username = "NAME"
password = "PW"
driverClassName = "oracle.jdbc.driver.OracleDriver"
factory = "org.apache.tomcat.dbcp.dbcp2.BasicDataSourceFactory"
url = "jdbc:oracle:thin:#127.0.0.1:1521:xe"
maxActive ="500"
maxIdle = "100"
/>
</Context>
This is my web.xml in WEB-INF/lib (of my current project)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>MemberBoard</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Connection</description>
<res-ref-name>jdbc/OracleDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
I didn't touch xml files in my tomcat 9.0 server.

Configuring JNDI JDBC for Web App in Tomcat

I'm unable to figure out why JNDI JDBC data source is failing
context.xml in META-INF
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true">
<Resource auth="Container"
name="jdbc/BigByte"
type="javax.sql.DataSource"
driverClassName="com.ibm.as400.access.AS400JDBCDriver"
url="jdbc:as400://****.****/****;prompt=false;sort=language;sort language=ENU;sort weight=shared"
username="****"
password="****"
maxIdle="10"
maxActive="200"
maxWait="5"
removeAbandoned="true"
removeAbandonedTimeout="1200" />
</Context>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
*
*
*
<resource-ref>
<description>Big Byte DB Connection</description>
<res-ref-name>jdbc/BigByte</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
my test
DataSource ds = null;
try {
Context initCtx = new InitialContext();
NamingEnumeration<NameClassPair> list = initCtx.list("");
while (list.hasMore()) {
System.out.println(list.next().getName());
}
Context envCtx = (Context)initCtx.lookup("java:comp/env");
ds = (DataSource)envCtx.lookup("jdbc/BigByte");
Connection con = ds.getConnection();
PreparedStatement ps = con.prepareStatement(
"select * from XAAQREV1 where AQABVN = ?");
ps.setString(1, "*****");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
String uid = rs.getString("AQABVN");
System.out.println(uid);
}
} catch (NamingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
My console output
2017-09-28 10:13:26,482/: [http-nio-8080-exec-10/:ERROR] - Cannot find the class org/apache/naming/LocalStrings.class
2017-09-28 10:13:26,619/: [http-nio-8080-exec-10/:ERROR] - Cannot find the class org/apache/naming/LocalStrings_en.class
2017-09-28 10:13:26,775/: [http-nio-8080-exec-10/:ERROR] - Cannot find the class org/apache/naming/LocalStrings_en_US.class
2017-09-28 10:13:28,091/: [http-nio-8080-exec-10/:ERROR] - Cannot find the class org/apache/juli/JdkLoggerConfig.class
javax.naming.NameNotFoundException: Name [java:comp/env] is not bound in this Context. Unable to find [java:comp].
at org.apache.naming.NamingContext.lookup(NamingContext.java:824)
at org.apache.naming.NamingContext.lookup(NamingContext.java:172)
at javax.naming.InitialContext.lookup(Unknown Source)
at webx0001.XAC3DFR_ObFnc.ObRun(XAC3DFR_ObFnc.java:189)
...
When I step through my test, the NamingEnumeration list has no elements.
What are the console messages about missing classes about?
What am I missing?
I was not missing anything.
My project uses a Custom ClassLoader that breaks Tomcat JNDI.
I verified that with a simple servlet in a simple project.

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