I have a problem to get connection to my database via java and jboss esb 4.2. I have build a .war package and deployed it on the jboss server. But i am gettin error while trying to connect to the database.
Java Method
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/VINService");
String sql = "SELECT ...";
conn = ds.getConnection();
stmt = conn.prepareStatement(sql);
...
jboss-web.xml
<resource-ref>
<res-ref-name>jdbc/VINService</res-ref-name>
<jndi-name>java:jdbc/VINService</jndi-name>
<res-type>javax.sql.DataSource</res-type>
</resource-ref>
web.xml
<resource-ref>
<description>VINService</description>
<res-ref-name>jdbc/VINService</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
VIN_Service-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/VINService</jndi-name>
<connection-url>jdbc:mysql://url/db?autoReconnect=true</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>name</user-name>
<password>password</password>
<min-pool-size>5</min-pool-size>
<max-pool-size>20</max-pool-size>
<idle-timeout-minutes>0</idle-timeout-minutes>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<check-valid-connection-sql>SELECT 1 FROM vehiclecache limit 1</check-valid-connection-sql>
</local-tx-datasource>
</datasources>
What am i doing wrong? I am getting Nullpointer Error at
conn = ds.getConnection()
so it isn't finding the datasource.
Can anybody help me?
Related
I'm using tomcat 8 and and deployed the ANT build war file to the tomcat which was previously deployed on Weblogic. I have sorted out most the things and deployment is success too on tomcat locally. However, when i'm trying to test the application thru Postman scripts, I'm having trouble getting the DB connections. The Error is as below:
Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:142)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:148)
//Later part of the error logs:
--------------------------------
Caused by: java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2224)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2104)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
... 53 more
Caused by: java.lang.NullPointerException
at oracle.jdbc.driver.OracleDriver.acceptsURL(OracleDriver.java:981)
at java.sql.DriverManager.getDriver(DriverManager.java:299)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2209)
... 57 more
Please help me to understand where the problem is. My configurations are as below:
web.xml:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/MatchDS_DEV</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Context.xml : under tomcat/webapp/manager
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
<Resource name="jdbc/DEV" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="USER"
password="Admin_1"
driverClassName="oracle.jdbc.xa.client.OracleXADataSource"
url="jdbc:oracle:thin:#//mcpsecv01-***/MCPRM01V"/>
</Context>
DAO Class:
import javax.sql.DataSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
Class XX{
DataSource dataSource;
private NamedParameterJdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource)
{
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
//more source code
//within a method, m calling this jdbcTemplate like this:
Object value = jdbcTemplate.query(params); // this is where it throws error.
//more source code
}//End of class X
I have placed below jars in tomcat/lib:
ojdbc14 , spring-jdbc-3.2.0.RELEASE , commons-dbcp2-2.1.1
I have been able to configure my datasource in a web app but am unable to replicate that in a stand alone app.
Datasource is configured as server.xml file in tomcat server in config folder. How can I load the datasource from this file in a stand alone Java application?
If I try with
Context initialContext = new InitialContext();
DataSource dataSource = (DataSource) csDBEnvContext.lookup(DATASOURCE_NAME);
Its gives the following exception:
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
Same code when tried using web application is executed fine.
username, password are redacted since it is production environment.
Here is the jdbc.java :
public static void main(String[] args) throws Exception {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/haryana");
//Connection conn = ds.getConnection();
try {
DatabaseMetaData dataSource = null;
conn = ds.getConnection();
} catch (Exception e) {
System.out.println("Exception while getting local JDBC Connection getLogsJdbcConnection(): " + e.getMessage());
}
Context.xml :
<Context crossContext="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource name="jdbc/haryana" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="postgres"
password="postgres"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/fps_ws_db_feb17"/>
</Context>
web.xml file :
<resource-ref>
<description>Database connection resource</description>
<res-ref-name>jdbc/haryana</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
can anyone please help me to rectify the this Exception.
I have an issue with mySql integration into Tomcat 8. I will detail my steps and the error that occurs below.
I have added the connector jar to catalina_home/lib
I have created a brand new database called testdb, and created a new web project called TCmySqlSetup
In Meta-inf/context.xml I added:
<Context antiJARLocking="true" path="/TCmySqlSetup">
<Resource name="jdbc/testdb"
auth="Container"
type="javax.sql.DataSource"
username="root"
password="myPass"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql:/localhost:3306/testdb"
maxActive="15"
maxIdle="3"/>
</Context>
Then I created web.xml
<web-app>
<resource-ref>
<description>Db Conn</description>
<res-ref-name>jdbc/testdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
Inside my index.JSP I have placed
// InitialContext ctx =null;
DataSource ds =null;
Statement stmt= null;
Connection con = null;
ResultSet rs = null;
// try{ Class.forName("com.mysql.jdbc.Driver").newInstance();
/* ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/testdb");*/
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
ds = (DataSource) envCtx.lookup("jdbc/testdb");
con = ds.getConnection();
stmt = con.createStatement();
I havent copied the whole code, because the problem doesnt lie there, it's just simple column extraction...
And then when I try to run the website I get this:
org.apache.jasper.JasperException: An exception occurred processing
JSP page /index.jsp at line 47
44: 45: 46: 47:
con = ds.getConnection(); 48: stmt =
con.createStatement(); 49: 50: rs =
stmt.executeQuery("select name from employee");
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.ServletException: java.sql.SQLException: Cannot create
JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL
'jdbc:mysql:/localhost:3306/MySQL56'
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
org.apache.jsp.index_jsp._jspService(index_jsp.java:223)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.sql.SQLException: Cannot create JDBC driver of class
'com.mysql.jdbc.Driver' for connect URL
'jdbc:mysql:/localhost:3306/MySQL56'
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2160)
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
org.apache.jsp.index_jsp._jspService(index_jsp.java:164)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.sql.SQLException: No suitable driver
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2151)
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
org.apache.jsp.index_jsp._jspService(index_jsp.java:164)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
I have been fighting with this for a long period of time and i really want to do it right, i have most of the code for my real environment and it seems that i can connect without pooling somehow with this code
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection conn;
conn = DriverManager.getConnection("jdbc:mysql://localhost/testdb","root","peskir123");
Statement sqlStatement = conn.createStatement();
Unable to get jndi data source.
Error :
javax.naming.NameNotFoundException: Name [jdbc/MyLocalDB] is not bound in this Context. Unable to find [jdbc]
Environment : netbeans 8.0 + tomcat 8 + jdk8
In tomcat server at /conf/context.xml added below block:
<Resource name="jdbc/MyLocalDB"
global="jdbc/MyDB"
type="javax.sql.DataSource" auth="Container"/>
In tomcat server at /conf/server.xml added below block:
<Resource name="jdbc/MyDB"
global="jdbc/MyDB"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:5432/ARBADMIN"
username="postgres"
password="postgres"
maxActive="100"
maxIdle="20"
minIdle="5"
maxWait="10000"/>
Kept postgresql-9.3-1103.jdbc3 to tomcat /lib folder
Java code :
ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/MyLocalDB");
con = ds.getConnection();
stmt = con.createStatement();
In netbeans ide whether i need to do any thing?
I tried to run project on tomcat 7.0.52 and initialize to DB through context.xml file.
But it throws bunch of exceptions, I couldn't figure out what is wrong there.
Here is console output:
java.sql.SQLException: com.mysql.jdbc.Driver
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:701)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:635)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:486)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:144)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)
at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:554)
at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:242)
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:141)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
Here is full stack trace.
Here is output to tomcat catalina log
snippet of web.xml:
<resource-ref>
<description>Travel Agency Datasource</description>
<res-ref-name>jdbc/onlinedb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
context.xml:
<Context>
<Resource name="jdbc/onlinedb"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
maxActive="20" maxIdle="10"
maxWait="-1"
username="root"
password="secret"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/travelagency?characterEncoding=utf8"/>
</Context>
ConnectionManager class:
public class ConnectionManager {
private static Logger log = Logger.getLogger(ConnectionManager.class);
public static Connection getConnection() throws SQLException {
Connection con = null;
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource datasource = (DataSource) envContext.lookup("jdbc/onlinedb");
con = datasource.getConnection();
} catch (NamingException e) {
log.error(e);
}
return con;
}
}
mysql-connector-java-5.1.27-bin.jar is added to cp:
I tried to change content of context.xml file:
<resource-env-ref>
<description>Travel Agency Datasource</description>
<resource-env-ref-name>jdbc/onlinedb</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
But it keep failing.
How to solve this trouble?
You have to add a MySQL jdbc driver to the classpath.
Either put a MySQL binary jar to tomcat lib folder or add it to we application WEB-INF/lib folder.
You can find binary jar (Change version accordingly): https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.27
When you encounter exceptions like this, the most useful information is generally at the bottom of the stacktrace:
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
...
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)
The problem is that Tomcat can't find com.mysql.jdbc.Driver. This is usually caused by the JAR containing the MySQL driver not being where Tomcat expects to find it (namely in the webapps/<yourwebapp>/WEB-INF/lib directory).
I use sprint-boot (2.1.1), and mysql version is 8.0.13. I add dependency in pom, solve my problem.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
MySQL Connector/J » 8.0.13 link: https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.13
MySQL Connector/J » All the version link:
https://mvnrepository.com/artifact/mysql/mysql-connector-java
I have also dealt with this exception after a fully working context.xml setup was adjusted. I didn't want environment details in the context.xml, so I took them out and saw this error. I realized I must fully create this datasource resource in code based on System Property JVM -D args.
Original error with just user/pwd/host removed:
org.apache.tomcat.jdbc.pool.ConnectionPool init
SEVERE: Unable to create initial connections of pool.
Removed entire contents of context.xml and try this:
Initialize on startup of app server the datasource object sometime before using first connection. If using Spring this is good to do in an #Configuration bean in #Bean Datasource constructor.
package to use: org.apache.tomcat.jdbc.pool.*
PoolProperties p = new PoolProperties();
p.setUrl(jdbcUrl);
p.setDriverClassName(driverClass);
p.setUsername(user);
p.setPassword(pwd);
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setValidationQueryTimeout(100);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(5);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(5);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
org.apache.tomcat.jdbc.pool.DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
ds.setPoolProperties(p);
return ds;