JAVA JDBC重用连接

时间:2013-03-13 10:37:21

标签: java jdbc

我有一个Java程序,我正在为选择查询做一些JDBC。每次inturns调用DBConnection()时我都应该调用testDataBase(),或者我应该为所有查询重用一个连接。提前谢谢。

private  void testDataBase(String query){
    Connection con = DBConnection();
    Statement st = null;
    ResultSet rs = null;

    try {
        st = con.createStatement();
        rs = st.executeQuery(query);
        boolean flag = true;
        while (rs.next()) {
            String resultString = "";
            for(int i = 1; i <=rs.getMetaData().getColumnCount();i++){
                resultString=resultString+" "+  rs.getString(i);
            }
            System.out.println(resultString);
        }
    } catch (SQLException e) {
        e.printStackTrace();

    } finally {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}               



private  Connection DBConnection() {
    final String method_name =  "DBConnection";
    Connection conn = null;
    try{
      Class.forName(driver).newInstance();
      conn = java.sql.DriverManager.getConnection(url,userName,password);

    }catch (ClassNotFoundException e) {
        System.out.println(e.getMessage());
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    return conn;
}

6 个答案:

答案 0 :(得分:10)

在性能方面,打开数据库连接是一项昂贵的操作。您应该使用ConnectionPool在不同请求之间共享连接。

答案 1 :(得分:5)

数据库连接是长时间运行的,应该重复使用,除非您的查询率非常低。

答案 2 :(得分:5)

连接不是线程安全的,因此跨请求共享它们不是一个好主意。

更好的想法是集中连接并尽可能缩小范围:检查池中的连接,使用它,在事务范围内关闭它。

答案 3 :(得分:4)

获取数据库连接是一项非常昂贵的操作,因此建议尽可能重用连接。还要考虑使用连接池,它将为您维护许多连接,因此您可以在需要时从池中获取一个连接。上面显示的方法可能不需要更改,它取决于您调用的DBConnection()方法。

答案 4 :(得分:1)

我完全赞同@Amir Kost,在性能方面,在你可以做的最慢的操作之一中打开数据库连接,如果你有限制性的实时限制,那么这可能是个大问题。 我不知道你是否使用框架,但一个好的做法是发布一个包装连接池的bean,每次你需要直接与db交互时,你得到当前的开放连接(通常是对应于所谓的“会话”)。 我建议你(即使你没有使用任何框架)来重现这种技术性。

答案 5 :(得分:-1)

如果你只想要一个Connection实例,你可以使用Singleton模式,你可以考虑:

public class Connector {

private static final String URL = "jdbc:mysql://localhost/";
private static final String LOGIN = "root";
private static final String PASSWORD = "azerty";
private static final String DBNAME = "videotheque";
private static Connector connector;
private static Connection connection;

private Connector() {
}

public synchronized static Connector getInstance() {
    if (connector == null) {
        connector = new Connector();
    }
    return connector;
}

public static Connection getConnection() {
    if (connection == null) {
        Connection c = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            c = DriverManager.getConnection(URL + DBNAME, LOGIN, PASSWORD);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return c;
    }
    return connection;
}

}

然后,您可以致电:Connector.getInstance().getConnection()

相关问题