Can I create connection pooling with data sources using sybase driver(jconn2.jar)? I am working on this for quite an amount of time but in vain. The exception raised is :
Exception in
thread "main" java.lang.UnsupportedOperationException
at org.apache.commons.dbcp.PoolingDataSource.getConnection (PoolingDataSource.java:161)
My program looks like this :
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
public class ManualPoolingDataSourceExample {
public static void main(
String[] args) {
String url = "jdbc:sybase:Tds:172.16.12.84:2048/lnk_common_dd";
System.out.println("Loading underlying
JDBC driver.");
try {
Class.forName("com.sybase.jdbc2.jdbc.SybDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Done.");
System.out.println("Setting up data source.");
DataSource dataSource = setupDataSource(url);
System.out.println("Done.");
Connection conn = null;
Statement stmt = null;
ResultSet rset = null;
try {
System.out.println("Creating connection.");
conn = dataSource.getConnection("sa","");
System.out.println("Done.");
System.out.println("Creating statement.");
stmt = conn.createStatement();
System.out.println("Executing statement.");
rset = stmt.executeQuery("select * from testdata");
System.out.println("Results:");
int numcols = rset.getMetaData().getColumnCount();
while(rset.next()) {
for(int i=1;i<=numcols;i++) {
System.out.print("\t" + rset.getString(i));
}
System.out.println("");
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
try { rset.close(); } catch(Exception e) { }
try { stmt.close(); } catch(Exception e) { }
try { conn.close(); } catch(Exception e) { }
}
}
public static DataSource setupDataSource(String connectURI) {
ObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,null);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
return dataSource;
}
}
And i've placed all the necessary jars in the classpath ie :
commons-dbcp1.1.jar
commons-collections.jar
commons-pool1.2.jar
jconn2.jar
j2ee.jar