TOMCAT 6 Problems with DataSourceFactory and SQLServerDriver - java

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?

Related

Java error configuring jdbc

I have a web java project that when running some pages give me the following error:
Caused by: org.apache.ibatis.exceptions.PersistenceException:
Error building SqlSession.
The error may exist in SQL Mapper Configuration
Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.datasource.DataSourceException: There was an error configuring JndiDataSourceTransactionPool. Cause: javax.naming.NameNotFoundException: Intermediate context does not exist: jdbc/DashDBtest
I have the credentials of the database, but I do not know how to configure it to work
This error may related to DataSource configuration. if you use Apache Tomcat AS, it provide three ways to configure DataSource in JNDI.
1- in your application: context.xml under META-INF directory.
2- in your server context.xml under Tomcat/conf directory.
3- define it at global level by setting both server.xml and context.xml
I prefer the third way, so for Oracle Database declare the JNDI resource in your in your Tomcat/conf/server.xml like:
<GlobalNamingResources>
<Resource name="jdbc/DATABASE_NAME"
auth=Container
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.driver.OracleDriver"
username="db_username"
password="db_password"
url="jdbc:oracle:thin:#//localhost:1521:XE"
/>
....
and for MySQL Database do:
<GlobalNamingResources>
<Resource name="jdbc/DATABASE_NAME"
auth=Container
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
username="db_username"
password="db_password"
url="jdbc:mysql://localhost:3306/DATABASE_NAME"
/>
....
And reference the JNDI resource link in your Tomcat/conf/context.xml :
<Context>
<ResourceLink name="jdbc/MY_DATABASE_NAME" global="jdbc/DATABASE_NAME" type="javax.sql.DataSource"/>
</Context>
and finally use the ResourceLink's name in your project to get the DataSource.
hope it help.

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"

Tomcat JNDI resource for Datanucleus (JDO)

I have developed an application on Tomcat 7.0 that uses Datanucleus / JDO to access to a database. I currently have the JDO connection properties stored in the "datanucleus.properties" located in the application itself. The connection is working fine, but I would like to store the connection information as JNDI, to have it on the server and no longer in the war itself (I always have to replace the file in the war when deploying it remotely).
I tried the following:
Create a in the web.xml of the application (jdbc/ConnectionDB)
In "Server.xml", I tried to add the following the context of my application
<Resource name="jdbc/ConnectionDB" auth="Container" type="javax.jdo.PersistenceManagerFactory" /> <ResourceParams name="jdbc/ConnectionDB
<parameter>
<name>javax.jdo.PersistenceManagerFactoryClass</name>
<value>org.datanucleus.api.jdo.JDOPersistenceManagerFactory</value>
</parameter>
<parameter>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost/TomcatTest</value>
</parameter>
...
I then try to create a new PMF with the following syntax:
Context context = null;
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("java:comp/env/jdbc/ConnectionDB",context);
When I run my application, I get a javax.jdo.JDOUserException: You have either specified for this PMF to use a "persistence-unit" of "datanucleus.properties" (yet this doesnt exist!)
I don't really understand what is wrong in my setup.
Regards,
Marcel
I finally found the solution I was looking for, I post it here, it might help somebody else:
Create a resource in "Context.xml" file of the server
<Resource name="jdbc/SyncTestDB"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="mysql"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/SyncTestDB"/>
Create a reference to that resource in the "web.xml" file of your application
<resource-ref>
<description>MySQL Database Connection</description>
<res-ref-name>jdbc/SyncTestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And finally get a Persistence Manager Factory using the JNDI connection:
PersistenceManagerFactory pmf;
Properties properties = new Properties();
properties.setProperty("datanucleus.ConnectionFactoryName","java:comp/env/jdbc/SyncTestDB");
Read the javadoc for JDOHelper.getPersistenceManagerFactory(String) and it is obviously not for passing in some JNDI data source string.
Read the docs for Tomcat and you will also see that specifying a datasource you do not provide JDO connection details.
You can equally specify a persistence.xml with that JNDI string for the "javax.jdo.option.ConnectionFactoryName" property. As per the JDO spec and DataNucleus/Tomcat docs then

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

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.

Extra Information in JNDI Definition

I'm developing a web application and it is to be deployed on Apache Tomcat 6.0. Application will be connecting a lot of databases (almost 25) so in order to manage the Connections, I'm using a context.xml file located under META-INF. So far so good, and here is what a Resource Definition looks like:
<Resource
name="jdbc/XX"
auth="Container"
type="javax.sql.DataSource"
username="XXX"
password="XXX"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:XXXX"
maxActive="8"
maxIdle="4"/>
Given this definition, a Connection object is created correctly.
What I'd like to know is if I am able to add extra information in this definition, such as projectName, and reach it from the context(or somewhere else). Something like the following:
<Resource
name="jdbc/XX"
auth="Container"
type="javax.sql.DataSource"
username="XXX"
password="XXX"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:XXXX"
maxActive="8"
maxIdle="4"
projectName="Cool Project"/>
Any help is appreciated..
No, but you could define a naming convention and add an Environment element for each of your resource :
<Environment name="XX_projectName"
value="Cool Project"
type="java.lang.String"
override="false"/>
In your code, you would access it via
Context ctx = new InitialContext();
String projectName = (String) ctx.lookup("java:comp/env/XX_projectName");
See http://tomcat.apache.org/tomcat-7.0-doc/config/context.html#Environment%20Entries for details.

Categories

Resources