Connection Pool in RESIN

the database section in official Resin documention descripes as follow linked here(http://www.caucho.com/resin-3.0/db/config.xtp).

connection pool
    A set of connections maintained so that the connections can be reused when there is a future need for the conneciton.
    Connection pools are used to reduce the overhead of using a database. Establishing a connection to the database is a costly operation. A connection pool keeps a pool of open connections, each connection can be used for a time as needed, and then released back to the pool. A connection that has been released back to the pool can then be reused.
    Connection pooling is especially important in server applications. The overhead of opening a new connection for each new client request is too costly. Instead, the database pool allows for a connection to be opened once and then reused for many requests.

DataSource
    A JDBC term (and interface name) used for a factory that is used to obtain connections.
    Resin provides an implementation of DataSource. Resin's implementation of DataSource is a connection pool.

Driver
     An implemetation of a defined interface that hides the details of communication with a device or other resource, such as a database.
    Driver provides an interface and is responsible for the communication with the database. Every different database (i.e Oracle, MySQL) has their own means of enabling communication from the client (in this case Resin and you applications) and the database. The Driver provides a common interface that hides the details of that communication.

Transaction
    A transaction is used to mark a group of operations and provide a guarantee that all of the operations happen, or none of them happen. Transactions protect the integrity of the database.
    Transactions are especially important in server applications where many threads of processing may be interacting with the database at the same time.

Obtaining and using a database connection
Getting the DataSource
The DataSource is a factory that is used to obtain a connection. The DataSource is obtained using the <jndi-name> specified when configuring the database resource.

Ideally, the JNDI lookup of DataSource is done only once, the DataSource obtained from the lookup can be stored in a member variable or other appropriate place. The stored DataSource can then be used each time a connection is needed. If it is not stored, there will be an impact on performance from having to do the lookup each time you want to get a connection.

Obtaining a DataSource  public class .... {
  private final static String DATASOURCE_NAME = "jdbc/test";
  DataSource _pool;
  ...
  void init()
  {
    try {
      Context env = (Context) new InitialContext().lookup("java:comp/env");
      _pool = (DataSource) env.lookup(DATASOURCE_NAME);
      if (_pool == null)
        throw new ServletException("`" + DATASOURCE_NAME + "' is an unknown DataSource");
    } catch (NamingException e) {
      throw new ServletException(e);
    }
  }
  ...
}


Getting a Connection
A connection is obtained from the DataSource. The connection is used as needed, and then released with a call to close() so that Resin knows it is available for a subsequent request.

It is very important that the close() is always called, even if there as an exception. Without the close(), Resin's database pool can loose connections. If you fail to close() a connection, Resin does not know that it is available for reuse, and cannot allocate it for another request. Eventually, Resin may run out of connections.

Always put a close() in a finally block, to guarantee that it is called.

The following example shows the use of a finally block that contains the close(). Because the close() is in a finally block, it will happen even if the code using the connection throws an exception.

Getting a connection from the DataSource 
    Connection conn = null;
    try {
      conn = pool.getConnection();

      Statement stmt = conn.createStatement();

      ResultSet rs = stmt.executeQuery(" ... ");

      ...

      rs.close();
      stmt.close();
    } catch (SQLException e) {
      throw new ServletException(e);
    } finally {
      try {
        if (conn != null)
          conn.close();
      } catch (SQLException e) {
      }
    } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值