connection pooling...

Is it possible to use connection pooling concept in core java application.... can anyone tell me the good reference site for connection pooling....

[154 byte] By [83Krisha] at [2007-11-27 10:30:33]
# 1

http://java.sun.com/products/jndi/tutorial/ldap/connect/pool.html

this might help u

AmitChalwade123456a at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...
# 2

http://www.javaworld.com/javaworld/jw-10-2000/jw-1027-pool.html

this one seems to be good than previous

AmitChalwade123456a at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...
# 3

i need to know whether connection pooling applicable only for j2ee application or it also applicable for core java application

83Krisha at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...
# 4

connection pooling is applicable for applications where multiple users will be accessing the database at the same time, so multiple connections will be made at the same time.

A standalone client application will most likely not do that. If so, you could always use a package like commons DBCP.

http://jakarta.apache.org/commons/dbcp/

gimbal2a at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...
# 5

Always use connection pooling. Non connection pooling to connect to a database is very very slow. No one should use it.

For connection pooling, you have a choice between ODBC and JDBC. Always use JDBC if you can find such a driver for your database. ODBC is older technology.

WIth JDBC you can create your own connection pooling object, or create a tag for it in the tomcat server (config.xml file), or you can use an ORM such as Hibernate (which would be way too advanced a topic for you to use at this point in time). I suggest at this point in time, creating your own.

George123a at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...
# 6

if anyone have sample java code for connection pooling concept..... pls forward to me....

83Krisha at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...
# 7

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.ConnectionPoolDataSource;

import javax.sql.PooledConnection;

public class MainClass {

public static void main(String[] args) {

Connection connection = null;

Statement statement = null;

ResultSet resultSet = null;

try {

connection = getConnection();

// Do work with connection

statement = connection.createStatement();

String selectEmployeesSQL = "SELECT * FROM employees";

resultSet = statement.executeQuery(selectEmployeesSQL);

while (resultSet.next()) {

printEmployee(resultSet);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

if (resultSet != null) {

try {

resultSet.close();

} catch (SQLException e) {

} // nothing we can do

}

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

} // nothing we can do

}

if (connection != null) {

try {

connection.close();

} catch (SQLException e) {

} // nothing we can do

}

}

}

private static Connection getConnection() throws NamingException, SQLException {

InitialContext initCtx = createContext();

String jndiName = "HrDS";

ConnectionPoolDataSource dataSource = (ConnectionPoolDataSource) initCtx.lookup(jndiName);

PooledConnection pooledConnection = dataSource.getPooledConnection();

return pooledConnection.getConnection(); // Obtain connection from pool

}

private static InitialContext createContext() throws NamingException {

Properties env = new Properties();

env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.rmi.registry.RegistryContextFactory");

env.put(Context.PROVIDER_URL, "rmi://localhost:1099");

InitialContext context = new InitialContext(env);

return context;

}

private static void printEmployee(ResultSet resultSet) throws SQLException {

System.out.print(resultSet.getInt("employee_id")+", ");

System.out.print(resultSet.getString("last_name")+", ");

System.out.print(resultSet.getString("first_name")+", ");

System.out.println(resultSet.getString("email"));

}

}

while running the above sample code.. javax.naming.ServiceUnavailableException arises.... anyone give me the solution thanks in advance

83Krisha at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...
# 8

InitialContext initCtx = createContext() gets the context from the tomcat container that the code is running in. Your code is using main() which means its runnning outside of a tomcat container enviornment and will never work. Likewise, JNDI works within the tomcat container. If you want to use main(), you have to crate a dataSource object locally. Sorry, not enough time to show an example.

George123a at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...
# 9

By the way, even with a single user on your application, still use connection pooling. Getting a connection without a pool takes 1 to 2 seconds each time the user updates or queries the database, with connection pooling, it will take only milliseconds.

George123a at 2007-7-28 18:03:48 > top of Java-index,Java Essentials,Java Programming...