Am I closing this connection to the Oracle pool correctly?

I am trying to use a federated connection for my Java web application. I am using an Oracle database and here is my code:

public class DatabaseHandler
{

    static private Connection m_database = null;

    static private OracleConnectionPoolDataSource pooledSource = null;

    /**
     * Attempts to open an Oracle database located at the specified serverName and port.
     * @param serverName Address of the server.
     * @param portNumber Port to connect to.
     * @param sid SID of the server.
     * @param userName Username to login with.
     * @param password Password to login with.
     * @throws WebApplicationException with response code 500 Internal Server Error.
     */
    static public void openDatabase(String userName, String password,String serverName,int portNumber, String sid)
    throws WebApplicationException
    {
        try
        {
            // Load the JDBC driver
            String driverName = "oracle.jdbc.driver.OracleDriver";
            Class.forName(driverName);

            // Create a connection to the database
            String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
            pooledSource = new OracleConnectionPoolDataSource();

            pooledSource.setUser(userName);
            pooledSource.setURL(url);
            pooledSource.setPassword(password);
            m_database = pooledSource.getConnection();

        }
        catch (ClassNotFoundException e) 
        {
            // Could not find the database driver
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
        catch (SQLException e) 
        {
            // Could not connect to the database
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
    }


    /**
     * Attempts to execute the specified SQL query.
     * @throws WebApplicationException with a response code of Bad Request
     * if the query is invalid SQL.
     */
    static public ResultSet makeQuery(String query) throws WebApplicationException
    {
        ResultSet rs = null;
        if (m_database != null)
        {
            try 
            {
                Statement stmt = m_database.createStatement();
                rs = stmt.executeQuery(query);
            }
            catch (SQLException e)
            {
                // invalid query
                System.out.println(query);
                throw new WebApplicationException(Response.Status.BAD_REQUEST);
            }
        }        
        return rs;
    }

    /**
     * Attempts to close the database. 
     * @throws WebApplicationException with a response code of  500 Server error
     */
    static public void closeDatbase() throws WebApplicationException
    {
        try
        {
            m_database.close();
            pooledSource.close();
        }
        catch(SQLException e)
        {
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
    }
}

I am doing this in Eclipse and I have a warning that it is pooledSource.close()deprecated. I have never used a merged connection before, and I just want to make sure that I'm doing everything right. Is there a better way to close an Oracle pool?

+3
source share
2 answers

An outdated method means that this method should not be used. In future releases, the method close()may be completely cleared. I suggest deleting pooledSource.close().

, Connection DataSource, . ResultSet, Connection , finally.

+6
  • .
  • finally.
  • - . . - .
  • , . . closeDatabase(), .

: connection connection pool .

:

public void doSomethingWithDb(Connection con, ...) {
    boolean release = (con == null);

    try {
        con = PersistenceUtils.getConnection(con); //static helper return a new conenction from pool when provided con==null otherwise simply returns the given con

        //do something

        if(release) {
            con.commit();
        }
    }
    catch(SQLException e) {
        //handle errors, i.e. calling con.rollback() but be sure to check for con!=null before. Again maybe null-safe static helper method here.
    }
    finally {
        if(release && con!=null){
            con.close();
        }
    }
}

Connection , db. null Connection, .

"DB-Method" "DB-Method", , .

, JDBC . , Utility-Class, PersistenceUtils, , commit (Connection), rollback (Connection), getConnection (Connection), close (Connection)... , - .

+1

All Articles