views:

36

answers:

3

I have a website, in which currently I am getting 1000 page views. I am expecting it will go around 30k per day in future. Now the problem for me to manage the DB connections. At present I am just connecting to DB directly from java program. I know it is worst design in the world. But for time being I have written like that. I have plan to manage connection pooling using JNDI. But the problem is my hosting provider is not supporting JNDI.

Can anyone suggest me how to manage DB connections without jndi?

+1  A: 

Connection pooling does not per se require the connections to be obtained by JNDI. You can also just setup and use a connection pool independently from JNDI. Let's assume that you'd like to use C3P0, which is one of the better connection pools, then you can find "raw" JNDI-less setup details in this tutorial.

Here's an extract of the tutorial:

ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "org.postgresql.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:postgresql://localhost/testdb" );
cpds.setUser("swaldman");
cpds.setPassword("test-password"); 

Create the datasource once during application's startup and store it somewhere in the context. The connection can then be acquired and used as follows:

Connection connection = null;
// ...

try {
    connection = cpds.getConnection();
    // ...
} finally {
    // ...
    if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}

Yes, closing in finally is still mandatory, else the connection pool won't be able to take the connection back in pool for future reuse and it'll run out of connections.

BalusC
A: 

Thank u very much BalusC. i have implemented practically in my local environment.its working really great.i will deploy today in godaddy server.i hope it will wrk fine..i will get back u if i face any problem.

MAHESH
A: 

the code in the server is working superb....thanq Balusc...you have saved my time

MAHESH