java 关闭oracle连接_java – 我是否正确关闭了这个Oracle池连接?

我试图在

Java中使用池连接到我的Web应用程序.我正在使用Oracle数据库,这是我的代码:

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);

}

}

}

我在Eclipse中这样做,我发出警告,pooledSource.close()已被弃用.我以前从未使用过汇集连接,我只是想确保我正在做的一切正确.有没有更好的方法来关闭Oracle池化资源?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值