What is the Singleton patten?

I want to make a bean for JDBC connection pool. And the Singleton patten is the best way. But my code is something wrong, who can help me to finish it?

import java.util.*;

import java.io.*;

import java.sql.*;

import javax.sql.*;

import oracle.jdbc.driver.*;

import oracle.jdbc.pool.*;

import oracle.sql.*;

import java.net.*;

privateclass OracleConnectionPoolingBean

{

// static reference to singleton class

privatestatic OracleConnectionPoolingBean database =null;

private PooledConnection pc=null;

private Connection conn =null;

private OracleConnectionPoolingBean()

{

init();

}

privatesynchronizedvoid init()

{

// initialize database here

Properties prop=new Properties();

try

{

URL url=this.getClass().getResource("OracleDbConnectionBean.class");

String path=url.getFile().substring(1,url.getFile().length()-"OracleDbConnectionBean.class".length())+"db.properties";

InputStream is=new FileInputStream(path);

//InputStream is=getClass().getResourceAsStream("/db.properties");

prop.load(is);

if(is!=null) is.close();

}

catch(IOException e)

{

System.out.println("db.properties can't open!");

}

String jdbc=prop.getProperty("drivers");

String url=prop.getProperty("url");

String user=prop.getProperty("user");

String password=prop.getProperty("password");

try

{

OracleConnectionPoolDataSource ocpds =new OracleConnectionPoolDataSource();

ocpds.setURL(url);

ocpds.setUser(user);

ocpds.setPassword(password);

pc = ocpds.getPooledConnection();

}

catch(Exception e)

{

System.out.println("Can't build a connectionpool!");

}

}

privatestatic PooledConnection getPoolingConnection()

{

// check if database has been constructed

if(database ==null)

database =new OracleConnectionPoolingBean();

return database.pc;

}

}

[4160 byte] By [HongX] at [2007-9-26 3:47:46]
# 1

Hi Hongx,

This class is a Singleton that provides access to one or many

connection pools defined in a Property file. A client gets

access to the single instance through the static getInstance()

method and can then check-out and check-in connections from a pool.

When the client shuts down it should call the release() method

to close all open connections and do other clean up.

Please use public instead of private class

public class OracleConnectionpoolingBean

{

}

I hope this will help you out.

Tirumalarao

Developer Technical Support

Sun Microsystems,

http://www.sun.com/developers/support

rao_indts at 2007-6-29 12:30:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi rao indts,

When I make the above code in my Jsp as a bean, the constructor must use public instead of private(public OracleConnectionPoolingBean()

), or it will some mistakes. But if I change it to public, is it still a singleton patten? And does it just one instance of OracleConnectionPoolingBean built? What should the bean's scope application or session?

HongX at 2007-6-29 12:30:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Help me! Thanks.
HongX at 2007-6-29 12:30:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Try this HongXreplace the init() with the static initialiser and have a public constructor which does nothing. You may have to make some variables as static... Dhina
ptdhina at 2007-6-29 12:30:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

1. I'd recommend keeping the constructor private, this way the pool cannot be instantiated directly. In this example you're using what some people call a lazy constructor. The static reference to OracleConnectionPoolingBean is only instantiated when the getPooledConnection method is called, and that's the way you want it, you don't want the pool constructed any other way.

2. You're only allowing for 1 connection in your "pool". In the getPooledConnection() you should do this

return database.opcds.getPooledConnection();

You'll have to make opcds a class member variable so you can reference it from both init() and getPooledConnection().

3. You shouldn't even keep a direct reference to a physical Connection, in other words, get rid of the PooledConnection pc class member.

For more info on the Oracle drivers see http://technet.oracle.com/docs/products/oracle8i/doc_library/817_doc/java.817/a83724/toc.htm .

You'll probably want to use the OracleConnectionCacheImpl class which allows you to specify a max pool size and has different caching schemas available. See the doc above...

-Derek

beattris at 2007-6-29 12:30:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Hi beattris,

I think I successed.

First when I use above code in Jsp as a ConnectionPooling bean. Just one person can access the database at the same time. So I think maybe this is singleton patten. Then I changed my code a little.

The code:

package bgi.database.search;

import java.util.*;

import java.io.*;

import java.sql.*;

import javax.sql.*;

import oracle.jdbc.driver.*;

import oracle.jdbc.pool.*;

import oracle.sql.*;

import java.net.*;

public class OracleConnectionPoolingBean

{

// static reference to singleton class

private static OracleConnectionPoolingBean database = null;

private PooledConnection pc=null;

private Connection conn = null;

private Statement stmt=null;

private ResultSet rset=null;

private OracleConnectionPoolDataSource ocpds;

public OracleConnectionPoolingBean()

{

init();

}

private synchronized void init()

{

// initialize database here

Properties prop=new Properties();

try

{

URL url=this.getClass().getResource("OracleDbConnectionBean.class");

String path=url.getFile().substring(1,url.getFile().length()-"OracleDbConnectionBean.class".length())+"db.properties";

InputStream is=new FileInputStream(path);

prop.load(is);

if(is!=null) is.close();

}

catch(IOException e)

{

System.out.println("db.properties can't open!");

}

String jdbc=prop.getProperty("drivers");

String url=prop.getProperty("url");

String user=prop.getProperty("user");

String password=prop.getProperty("password");

try

{

ocpds = new OracleConnectionPoolDataSource();

ocpds.setURL(url);

ocpds.setUser(user);

ocpds.setPassword(password);

}

catch(Exception e)

{

System.out.println("Can't build a connectionpool!");

}

}

public static synchronized OracleConnectionPoolDataSource getPoolingConnection()

{

// check if database has been constructed

if(database == null)

database = new OracleConnectionPoolingBean();

return database.ocpds;

}

public Statement getLogicalConnection()

{

try

{

pc = this.getPoolingConnection().getPooledConnection();

conn=pc.getConnection();

stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

}

catch(Exception e)

{

System.out.println("Connect Logical Error"+e);

}

return stmt;

}

public void executeOracleQuery(String query)

{

try

{

rset=this.getLogicalConnection().executeQuery(query);

}

catch(Exception e)

{

System.out.println("Execute query Error!!!"+e);

}

}

public ResultSet getResultSet()

{

return rset;

}

public void getFirstPos()

{

try

{

rset.first();

}

catch(Exception e)

{

System.out.println("OutPosition!!!"+e);

}

}

public void CloseLogicalConn()

{

try

{

stmt.close();

stmt = null;

conn.close();

conn = null;

}

catch(SQLException e)

{

System.out.println("DB close ERROR!"+e);

}

}

}

Thank you, beattris, rao indts and ptdhina!!!

HongX at 2007-6-29 12:30:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Hi Hongx, Your modyfied code is correct , so no fault in code. Please check the driver .I hope this will help you out. Tirumalarao Developer Technical Support Sun Microsystems, http://www.sun.com/developers/support
rao_indts at 2007-6-29 12:30:44 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...