DataSource如何关闭Connection

十月的星空 发布于 2014/05/07 10:04
阅读 1K+
收藏 0

 

package com.msi.druid;


import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.msi.model.User;

public class DBUtilsHelper {
 private DataSource ds = null;
 private QueryRunner runner = null;

 public DBUtilsHelper() {
  try {
   this.ds = DbPoolConnection.getInstance().getDataSource();
  } catch (SQLException e) {
   e.printStackTrace();
  }
  if (this.ds != null) {
   this.runner = new QueryRunner(this.ds);
  }
 }

 public DBUtilsHelper(DataSource ds) {
  this.ds = ds;
  this.runner = new QueryRunner(this.ds);
 }

 public QueryRunner getRunner() {
  return this.runner;
 }

 public static void main(String args[]) throws SQLException {

  DBUtilsHelper dbh = new DBUtilsHelper();
  QueryRunner runner = dbh.getRunner();

  List<User> arrayListResult = runner.query("select * from t_user",
    new BeanListHandler<User>(User.class));
  for (int i = 0; i < arrayListResult.size(); i++) {
   User user = arrayListResult.get(i);
   System.out.println(user);
  }

  List<Object[]> ObjectResult = runner.query("select * from t_user",
    new ArrayListHandler());

  for (int i = 0; i < ObjectResult.size(); i++) {
   for (int j = 0; j < ObjectResult.get(i).length; j++) {
    System.out.print(ObjectResult.get(i)[j] + "    ");
   }
   System.out.println();
  }

 }
}

 

 

package com.msi.druid;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.msi.model.User;

public class DbPoolConnection {

 private static DbPoolConnection databasePool = null;
 private static DruidDataSource dds = null;
 static {
  Properties properties = loadPropertyFile("db_server.properties");
  try {
   dds = (DruidDataSource) DruidDataSourceFactory
     .createDataSource(properties);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private DbPoolConnection() {

 }

 public static synchronized DbPoolConnection getInstance() {
  if (null == databasePool) {
   databasePool = new DbPoolConnection();
  }
  return databasePool;
 }

 public DruidDataSource getDataSource() throws SQLException {
  return dds;
 }

 public DruidPooledConnection getConnection() throws SQLException {
  return dds.getConnection();
 }

 public static Properties loadPropertyFile(String fullFile) {
  String webRootPath = null;
  if (null == fullFile || fullFile.equals(""))
   throw new IllegalArgumentException(
     "Properties file path can not be null : " + fullFile);
  webRootPath = DbPoolConnection.class.getClassLoader().getResource("\\")
    .getPath();
  webRootPath = new File(webRootPath).getParent();
  InputStream inputStream = null;
  Properties p = null;
  try {
   String profilepath = webRootPath + File.separator + fullFile;
   System.out.println(profilepath);
   inputStream = new FileInputStream(new File(profilepath));
   p = new Properties();
   p.load(inputStream);
  } catch (FileNotFoundException e) {
   throw new IllegalArgumentException("Properties file not found: "
     + fullFile);
  } catch (IOException e) {
   throw new IllegalArgumentException(
     "Properties file can not be loading: " + fullFile);
  } finally {
   try {
    if (inputStream != null)
     inputStream.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  return p;
 }

 

上面的代码使用DBUtils +Druid形成了一个数据库连接池,请教下,使用了DataSource之后,如何关闭Connection呢(不要是显示关闭Connection,因为上述代码无Connection)

加载中
0
excepiton
excepiton

DruidPooledConnection  这个类应该有实现close方法

十月的星空
十月的星空
沒看到文檔說Druid 會自動關閉Connection
0
Ryan-瑞恩
Ryan-瑞恩

这样关闭:DruidPooledConnection.close();

Ryan-瑞恩
Ryan-瑞恩
回复 @十月的星空 : 查API,及代码吧!
十月的星空
十月的星空
都沒出現過類似Connection conn= DruidPoolConnection.getConnection 的代碼 還需要DruidPoolConnection.closeConnection??? 你说的那样有点说不通
0
l
lee806464780

直接释放就行了,把引用的置空。

DruidPooledConnection con = DruidPoolConnection.getConnection();

释放的时候con = null

十月的星空
十月的星空
查看了源碼,系統自動關閉
返回顶部
顶部