Issue
I have a simple web service that is trying to open a connection to the database.
I am using jax-ws, Oracle 12c DB, Tomcat 9, Java 8.
Can anyone post the correct way to do this?
So far I've downloaded ojdbc7.jar and ucp.jar into the $TOMCAT_HOME/lib directory.
- What goes in applicationPath/META-INF/web.xml ?
Right now I have the following:
<resource-ref>
<res-ref-name>jdbc/UCPPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
- What goes in TOMCAT_HOME/conf/server.xml ?
Right now I have the following:
<context docbase="demods" path="/demods" reloadable="true">
<Resource
name="jdbc/UCPPool"
auth="Container"
factory="oracle.ucp.jdbc.PoolDataSourceImpl"
type="oracle.ucp.jdbc.PoolDataSource"
description="Pas testing UCP Pool in Tomcat"
connectionFactoryClassName="oracle.jdbc.pool.OracleDataSource"
minPoolSize="2"
maxPoolSize="5"
inactiveConnectionTimeout="20"
user="test"
password="test"
url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracleDB12c)(PORT=1563))(CONNECT_DATA=(SERVICE_NAME=PPS)))"
connectionPoolName="UCPPool"
validateConnectionOnBorrow="true"
sqlForValidateConnection="select 1 from DUAL" />
</context>
What goes in TOMCAT_HOME/conf/context.xml ?
I don't have anything in this file right now.
Here is the code that I am using to setup the JDBC connection:
Context ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/UCPPool");
Connection conn = null;
try {
conn = ds.getConnection();
}
catch (SQLException e) {
return e.getMessage();
}
The message returned by the web service is:
"Cannot create JDBC driver of class '' for connect URL 'null'"
What am I doing wrong?
Thanks in advance!
Solution
Good news, I was able to get this to work today at the office.
Here is what I did:
- I created a context.xml file and placed it in my eclipse project inside of META-INF/. In META-INF/context.xml I put the following code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<Resource
name="jdbc/orcljdbc_ds"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
username="user1"
password="password"
url="jdbc:oracle:thin:@//myoracledb:1563/DEVSID"
/>
</Context>
I did not put anything in the Tomcat context.xml or server.xml file, but I think that you can for different scenarios (i.e.Shared datasource with multiple applications running on single application server). I also did not put anything in my Eclipse project WEB-INF\web.xml.
I used the following code to test the connection:
package planAdminWS;
import javax.jws.WebMethod;
import javax.jws.WebService;
import java.io.PrintWriter;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
@WebService
public class PlanAdminWS {
@WebMethod
public String createPlan(String EIN, String PN)
{
return "Creating Plan: " + EIN + "/" + PN;
}
@WebMethod
public String updatePlan(String EIN, String PN) throws NamingException, SQLException {
// Get a context for the JNDI look up
DataSource ds = getDataSource();
Connection conn = null;
try {
conn = ds.getConnection();
if (conn != null) {
Statement stmt = null;
ResultSet rs = null;
String query = "select * from V$version";
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
System.out.println(rs.getString(1));
return "Database Version : " + rs.getString(1);
}
} else {
return "Conn is NULL";
}
} catch (SQLException e) {
return e.getMessage();
}
return "Could not get DB Version";
}
/*
* Method to create a datasource after the JNDI lookup
*/
private DataSource getDataSource() throws NamingException {
Context ctx;
ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
// Look up a data source
javax.sql.DataSource ds
= (javax.sql.DataSource) envContext.lookup ("jdbc/orcljdbc_ds");
return ds;
}
}
this returned the following in SoapUI:
"Database Version : Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production"
Thanks for the help!!!
Here was another good resource: https://www.journaldev.com/2513/tomcat-datasource-jndi-example-java
Answered By - Aamir Siddiqui