JDBC Datasource in Tomcat

Hi

To enable data source in Tomcat 6 is very simple, All you have to do is to goto [Tomcat-Home]/conf/server.xml

[Context docBase=”TestDB” path=”/TestDB” reloadable=”true” source=”org.eclipse.jst.j2ee.server:TestDB]
[Resource auth=”Container” connectionCacheProperties=”{MaxStatementsLimit=10, MinLimit=3, InitialLimit=1, ValidateConnection=true, ConnectionWaitTimeout=10, MaxLimit=10}”
driverclassname=”oracle.jdbc.driver.OracleDriver” factory=”oracle.jdbc.pool.OracleDataSourceFactory”
name=”JNDI_NAME” password=”PASSWORD” scope=”Shareable”
type=”oracle.jdbc.pool.OracleDataSource” url=”jdbc:oracle:thin:@127.0.0.1:1521:MYDB”
user=”USER_NAME”/]
[/Context]

Now add the below API in your java class to get connection from this data source.

public OracleConnection getConnectionFromPool() throws SQLException,
NamingException {
String dataSourceName = “JNDI_NAME”;
Context initContext = new InitialContext();
DataSource ds=null;
try {
ds = (DataSource) initContext.lookup(“java:/comp/env/” + dataSourceName);
} catch (Exception e) {
throw e;
}
}
OracleConnection con = (OracleConnection) ds.getConnection();

return con;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s