开发者

Connection Pooling in Java [duplicate]

This question already has answers here: 开发者_JAVA技巧 How should I connect to JDBC database / datasource in a servlet based application? (2 answers) Closed 6 years ago.

I am trying to implement Connection Pooling in java.I have written one java code and one jsp file.When I run it on tomcat6.0.20,I got following error.

javax.servlet.ServletException: Cannot create JDBC driver of class '' for connect URL 'null'

on this line Connection conn = ds.getConnection();


Please follow steps.

1. Create a ConnectionManager class:

public class ConnectionManager {
 Vector connectionPool = new Vector(0, 1);
private static ConnectionManager obConnectionManager = new ConnectionManager();

    /**
     * TX Data Source.
     */
    DataSource obTXDataSource = null;

    /**
     * Non TX Data Source.
     */
    DataSource obNonTXDataSource = null;

    /**
     * A non tx data source created for using it in reports.
     * This has been created for using it in reports with non transactional connection.
     *
     * The existing non- transaction connection attribute and function is being called from
     * many modules. In order to have lesser impact this function and attribute is being introduced.
     * This has been done after the conference call had with Retesh - SMS
     */

    DataSource reportsNonTxDataSource = null;

    /**
     * Constuctor for the class.
     */
    private ConnectionManager() {
    }

    /**
     * Returns instance of the class.
     * @return Instance of the class
     */
    public static ConnectionManager getInstance() {
        return obConnectionManager;
    }

    /**
     * Returns the database connection using a transactional Datasource.
     * @return The database connection using a transactional Datasource
     * @exception PersistenceException
     */
    public Connection makeTXConnection()
        throws PersistenceException {
        try {
            if (obTXDataSource == null) {
                obTXDataSource = ServiceLocator.getTXDataSource();
            }
            Connection obConnection = obTXDataSource.getConnection();
            connectionPool.add(obConnection);
            return obConnection;
        } catch (ServiceLocatorException se) {
            throw new PersistenceException("PE002",
                "Error making database connection: " + se.getMessage());
        } catch (SQLException se) {
            throw new PersistenceException("PE002",
                "Error making database connection: " + (new Integer(se.getErrorCode())).toString() +
                se.getMessage());
        }
    }
}

2. Create a QueryManager class:

public class QueryManager {
    /**
     * This is an object of Debug class for locating error in the process.
     */
    private Debug debug = new Debug(QueryManager.class);

    /**
     * The PreparedStatement instance.
     */
    private PreparedStatement obPreparedStatement = null;

    /**
     * The Connection instance.
     */
    private Connection obConnection = null;

    private int procedureTimeOut = 0;//V3.2

    /**
     * Constuctor for the class.
     */
    public QueryManager() {
    }

    /**
     * Executes the SELECT statement.
     * 
     * @param sql
     *            The SELECT statement
     * @param params
     *            The parameters passed to the SELECT statement
     * @return the Array of Data fetched after executing the SELECT statement
     * @exception PersistenceException
     */
    public ArrayList executeSQL(String sql, Object[] params)
            throws PersistenceException {
        java.util.Date startDate = new java.util.Date();
        java.util.Date endDate = null;
        try {
            obConnection = ConnectionManager.getInstance()
                    .makeNonTXConnection();
            obPreparedStatement = obConnection.prepareStatement(sql);
            int length = 0;
            if (params != null) {
                length = params.length + 1;
            }
            for (int i = 1; i < (length); i++) {
                if (params[i - 1] == null) {
                    obPreparedStatement.setNull(i, java.sql.Types.VARCHAR);
                } else {
                    obPreparedStatement.setObject(i, params[i - 1]);
                }
            }
            ArrayList result = getResult();
            endDate = new java.util.Date();

            return result;

        } catch (SQLException se) {
            debug.log(Debug.ERROR, "SQL Exception", se);
            se.printStackTrace();
            throw new PersistenceException("PE003", createErrString(
                    se.getErrorCode(), se.getMessage()));

        } finally {
            closeConnection(obConnection, obPreparedStatement);
            if (endDate == null) {
                endDate = new java.util.Date();
            }
            debug.log(Debug.INFO, constructLogString(startDate, endDate, sql));
            startDate = endDate = null;

        }
    }
}

3. Call your query:

ArrayList result = null;
String query = "Select * From emp where empID=?";

try {
     Object[] params={1};
     result = queryManager.executeSQL(query,params);
} catch (PersistenceException e) {

}
return result;

4. Config context.xml (use JDBC Driver)


Have you configured the datasource?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜