Pages

Showing posts with label database connection in java. Show all posts
Showing posts with label database connection in java. Show all posts

Thursday, July 16, 2009

Maintaining database connection in Context.xml

Meta-inf folder -> context.xml

in this write the following code
--&lt-- ?xml version="1.0" encoding="UTF-8"? --&gt--
--&lt-- Context antiJARLocking="true" path="/search" --&gt--
--&lt-- !-- Default set of monitored resources -- --&gt--
--&lt-- WatchedResource --&gt-- WEB-INF/web.xml --&lt-- /WatchedResource --&gt--
--&lt-- !-- Uncomment this to disable session persistence across Tomcat restarts -- --&gt--
--&lt-- !--
--&lt-- Manager pathname="" / --&gt--
-- --&gt--
--&lt-- Resource auth="Container"
driverClassName="com.mysql.jdbc.Driver"
factory="org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory"
logAbandoned="true" maxActive="30"
maxIdle="10"
maxWait="1000"
name="jdbc/POS"
removeAbandoned="true"
removeAbandonedTimeout="60"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:3306/POC?autoReconnect=true"
username="root"
password="kmipl"/ --&gt--
--&lt-- /Context --&gt--

Note : replace all --&lt-- with '<' --&gt-- with '>'


______________________________________________________________


to get the connection from context.xml file just use the following method

public Connection dbConnectionCheck()
{
try
{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/POS");
connection = ds.getConnection();
}
catch(Exception e1)
{
System.out.println(e1);
}
return connection;

}

_______________________________________________________

by this we can change the database with out redeploying or rebuilding the war file.