Latest Entries »

Thursday, March 5, 2009

Apache Tomcat 6.0 JNDI Datasource

Configuration of JNDI

1. Create context.xml file in META-INF folder.











2. Create an entry for resource-ref in web.xml.











3. Make sure that the following JAR's added in your TOMCAT/lib
( C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.18\lib )
a. commons-collections-3.2.jar
b. commons-dbcp-1.2.2.jar
c. commons-pool-1.4.jar




















Note: for mysql add mysql-connector-java-5.1.5-bin.jar in TOMCAT/lib and for as400 add jt400.jar etc...









Java Code:
/*===========================================================================+
| Copyright (c) 2009 Sravan Modugula, |
| All rights reserved. |
+===========================================================================+
| HISTORY
| Created By : Sravan Modugula
| Date : 12-10-2008
| Description: class represents As400Connection
|
+===========================================================================*/

package com.sravan.modugula.as400database;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

public class As400Connection {

/**
* logger instance
*/
private static final Logger log = Logger.getLogger(As400Connection.class);

public static Connection getConnection() {
log.info(" Starting establish as400 connection");

Connection as400Connection = null;
try {
Context ctx = new InitialContext();
DataSource dataSource = (DataSource) ctx
.lookup("java:comp/env/jdbc/databaseAS400");
if (dataSource != null) {
as400Connection = dataSource.getConnection();
return as400Connection;
}
} catch (Exception e) {
e.printStackTrace();
log.fatal("Got Exception when getting as400 connection");
log.fatal("Exception is ", e);
}
// log.info(" ConnectionPtest Ending"+System.currentTimeMillis()/1000);
return as400Connection;
}

public static void closeConnection(Connection as400Connection) {
try {
if (as400Connection != null) {
as400Connection.close();
as400Connection = null;
}
} catch (SQLException e) {
log.error(e.getMessage(), e);
} finally {
if (as400Connection != null)
try {
as400Connection.close();
log.info("Closed connection");
} catch (SQLException e) {
log.error(e.getMessage(), e);
}
}
}

}

2 comments:

Web Developer said...

I do as you want but there are exception
DB connection was not aquired. (java.lang.UnsupportedOperationException: Not supported by BasicDataSource)
- SELECT * FROM publisher failed (null)

what I should do

Sravan Modugula said...

I think the problem is with dataSource.getConnection();
Check this piece of code; to diagnose the issue I need more information. With the limited information provided I guess you might be passing the parameters to get the connection and it is not supported by the basic datasource. I hope this information will help you to resolve the issue :), if not you can post your questions.
Good Day!!!