search

Home  >  Q&A  >  body text

java - DataSource 连接池用完之后是否需要关闭?

如题目描述,在对数据库操作完成之后是否需要关闭DataSource,还是只是关闭当前的Connection就可以了?

PHPzPHPz2889 days ago1044

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:46:55

    Connection needs to be closed when it is used up, regardless of whether the connection pool is used or not. If the connection pool is not used, Connection closing actually closes the database connection. If the connection pool is used, Connection closing actually puts the Connection back into the connection pool instead of actually closing the connection. The purpose of using the connection pool is to prevent frequent creation and closing of Connections. The connection pool will reuse the Connection, that is, reuse it multiple times.

    DataSource also needs to be closed after use, usually before the program stops. Most projects do not manually close the DataSource in the code because some frameworks usually do it for you. For example Spring configuration DataSource:

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    ...
    </bean>

    destroy-method="close" means to close the DataSource before the end of the DataSource life cycle.

    reply
    0
  • PHPz

    PHPz2017-04-17 17:46:55

    The Connection needs to be closed after a single database operation is completed. The DataSource may need to be closed before the program ends, but generally there is no need to do this explicitly.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:46:55

    These can be left to the framework to handle, mybatis-spring

    reply
    0
  • Cancelreply