Reading tomcat server.xml in junit test case - java

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.

Related

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"

How to Setup Jndi with weblogic server

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.

How to get PoolingDataSource from Tomcat's JNDI

If I deploy a web app to Tomcat, and have code like this:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/myDB");
How can I specify for this DataSource to be a PoolingDataSource? How do I configure the pool (GenericObjectPool) to inject the PoolingDataSource with?
Or, is this the default behavior of Tomcat's JNDI implementation? Thanks in advance!
Just configure the connection pool settings (maxActive, maxWait, maxIdle, ...).
Tomcat comes with the apache commons-dbcp library. It is repackaged as $CATALINA_HOME/lib/tomcat-dbcp.jar.
Take a look at the tomcat doc for details: http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html#Database_Connection_Pool_%28DBCP%29_Configurations
<Context>
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest"/>
</Context>
The JDBC Connection Pool org.apache.tomcat.jdbc.pool
is a replacement or an alternative to the commons-dbcp
connection pool.
Add to the server.xml file in the GlobalNamingResources section something like the next:
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="100"
minIdle="10"
maxWait="10000"
initialSize="10"
removeAbandonedTimeout="60"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="root"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mysql"/>
Add the JDBC driver JAR into the Tomcat lib directory.
Add in your context.xml:
<ResourceLink global="jdbc/TestDB" name="jdbc/TestDB"
type="javax.sql.DataSource"/>
See more in The Tomcat JDBC Connection Pool.

When is the tag I added in context.xml gets read?

In the context.xml of Tomcat I added the following tag :
<Resource name="jdbc/MyDatasource" auth="Container" type="javax.sql.DataSource"
driverClassName="org.apache.derby.jdbc.ClientDriver"
url="jdbc:derby://localhost:1527/my_database;create=true"
username="me" password="me"
maxActive="20" maxIdle="10" maxWait="-1" />
When does the following tag gets read ? Is it read when i start the server ?
This configuration is read when the application is deployed. If you have auto-deploy enabled, Tomcat will deploy your webapp during startup.

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