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"
Related
I am running a web application with Tomcat - JDBC connection pooling with Informix database. How do I set lock mode to wait?
You can also set the default lock time using the 'IFX_LOCK_MODE_WAIT' connection string property (for datasources use 'ds.setIfxIFX_LOCK_MODE_WAIT()')
More info here:
https://www.ibm.com/support/knowledgecenter/en/SSGU8G_12.1.0/com.ibm.jdbc_pg.doc/ids_jdbc_034.htm
so, for tomcat, if your datasource looks something like:
<Context path="/jspdemo" docBase="jspdemo" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/jspdemo" auth="Container" type="javax.sql.DataSource" maxActive="20"
maxIdle="10" maxWait="1000" username="informix" password="mypasswd"
driverClassName="com.informix.jdbc.IfxDriver"
url="jdbc:informix-sqli://mymachine:1526/stores_demo:INFORMIXSERVER=ol_myserver"/>
</Context>
just use:
<Context path="/jspdemo" docBase="jspdemo" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/jspdemo" auth="Container" type="javax.sql.DataSource" maxActive="20"
maxIdle="10" maxWait="1000" username="informix" password="mypasswd"
driverClassName="com.informix.jdbc.IfxDriver"
url="jdbc:informix-sqli://mymachine:1526/stores_demo:INFORMIXSERVER=ol_myserver;IFX_LOCK_MODE_WAIT=60;/>
</Context>
In my environment the first SQL executed after obtaining db connection from pool is:
SET LOCK MODE TO WAIT 15
"How do I set this value via JDBC connection"
As per previous answers either pass it as a connection property or execute the SQL at the start of the connection.
I was able to break down the rds-combined-ca-bundle.pem cert file and import each one into the keystore separately. Then I added the -Djavax.net.ssl.trustStore=path_to_truststore_file
and -Djavax.net.ssl.trustStorePassword=password into the jvm options. It worked on one application using the jndi configuration such as below:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/contextname" unpackWAR="true" useNaming="true" swallowOutput="false">
<Resource removeAbandoned="true"
removeAbandonedTimeout="60"
name="jdbc/data" auth="Container"
type="javax.sql.DataSource"
maxActive="200"
maxIdle="60"
maxWait="20000"
username="rootuserssl"
password="rootusersslpassword"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://myinstance.123456789012.us-east-1.rds.amazonaws.com:3306/dbname?autoReconnect=true&verifyServerCertificate=true&requireSSL=true&useSSL=true"/>
</Context>
However, on one application using HikariCp, it generates an error javax.net.ssl.SSLException: Unsupported record version Unknown-0.0. Below is the configuration.
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/contextnametwo" unpackWAR="true" useNaming="true" swallowOutput="false">
<Resource name="jdbc/data" auth="Container"
driverClassName="com.mysql.jdbc.Driver"
jdbcUrl="jdbc:mysql://myinstance.123456789012.us-east-1.rds.amazonaws.com:3306/dbname?verifyServerCertificate=true&useSSL=true&requireSSL=true"
factory="com.zaxxer.hikari.HikariJNDIFactory"
type="javax.sql.DataSource"
maximumPoolSize="50"
connectionTestQuery="SELECT 1"
idleTimeout="300000"
maxLifetime="600000"
dataSource.implicitCachingEnabled="true"
dataSource.cachePrepStmts="true"
dataSource.prepStmtCacheSize="250"
dataSource.prepStmtCacheSqlLimit="2048"
dataSource.useServerPrepStmts="true"
catalog="dbname"
username="rootuserssl"
password="rootusersslpassword"
/>
</Context>
What am I doing wrong on the application that uses HikariCp?
Solved this one by using a mariadb connector (https://downloads.mariadb.org/connector-java/) instead of the mysql. Worked like a charm.
I am using java 7, and tomcat 7. I am writing few tests for my application in jUnit which uses tomcat/conf/server.xml for jndi. Here is the maven suggest folder structure.
src
|___test
|___java
| |___Testcase.java
|___resources
|___conf
|___server.xml
My sample server.xml would look like this,
<Resource name="jdbc/junit_db"
type="javax.sql.DataSource"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/junit_db?zeroDateTimeBehavior=round&autoReconnect=true&dumpQueriesOnException=true"
username="root"
password="password"
maxIdle="0"
minIdle="0"
initialSize="1"
maxWait="5000"
maxActive="50"
loginTimeout="1000"
minEvictableIdleTimeMillis="2000"
timeBetweenEvictionRunsMillis="5000"
validationQuery="SELECT 1"
testOnBorrow="true"
testOnReturn="true"
testWhileIdle="false"
logAbandoned="true"
removeAbandoned="true"
poolPreparedStatements="true"
maxOpenPreparedStatements="10000"
accessToUnderlyingConnectionAllowed="false"
defaultAutoCommit="false"
defaultReadOnly="false"
defaultTransactionIsolation="4"/>
<Resource name="jdbc/junit_hive_db"
type="javax.sql.DataSource"
factory="com.office.hive.HiveDataSourceFactory"
driverClassName="org.apache.hive.jdbc.HiveDriver"
url="jdbc:hive2://localhost:10000/default?zeroDateTimeBehavior=round"
username=""
password="" />
I want to load this server.xml into the IntialContext before running jUnit test cases. How to achieve this?
Followed this link, it has solution for loading jndi into initialcontext manually.
http://www.alexecollins.com/tomcat-context-junit-rule/
Give TomcatJNDI a try. When fed with Tomcat's configuration files it will deliver all JNDI based objects that are declared in these files as soon as they are looked up. The code to achieve this is for example
TomcatJNDI tomcatJNDI = new TomcatJNDI();
tomcatJNDI.processServerXml(serverXmlFile)
tomcatJNDI.processContextXml(contextXmlFile);
tomcatJNDI.start();
Then you can lookup the objects as you are used to:
DataSource ds = (DataSource) InitialContext.doLookup("java:comp/env/path/to/datasource")
More about TomcatJNDI can be found here.
I am using connection pooling in Struts2 application but after some request, I am not getting any result even though I am closing the connection in our application. Please give me proper solution.
Context.xml
<Resource name="jdbc/abbas"
auth="Container"
type="javax.sql.DataSource"
username="myuser"
password="pass#123"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.14.65/abbas"
maxActive="15"
maxIdle="3"/>
I am new to weblogic server.I have setup JNDI with tomcat server and it runing fine for me .But i have to setup JNDI with weblogic so please anyone help me where i have put
<ResourceLink global="jdbc/myPath" name="jdbc/myPath"
type="oracle.jdbc.pool.OracleDataSource" />
This code i.e in tomcat i have put this code in Context.xml file but not know where to add this in weblogic and this code also:
<Resource name="jdbc/myPath" auth="Container"
type="oracle.jdbc.pool.OracleDataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory" url="url"
user="username" password="password" maxActive="20" maxIdle="10"
maxWait="10000" />
This code is in server.xml file of tomcat but not know where to put in weblogic.Please help me
Thanks
You go to http://localhost:7001/console/. The default credentials are weblogic/welcome1. Then you access the data sources section and create your data source.