简体   繁体   English

JDBC连接池建议

[英]JDBC connection pooling advice

I am developing an application in java for processing data and uploading it to a MySQL database. 我正在开发一个java应用程序,用于处理数据并将其上传到MySQL数据库。 With this application, I iterate through all of the files that need processed in a directory. 使用此应用程序,我遍历需要在目录中处理的所有文件。 With each row of the file, I store the data from that row into a variety of variables and do some transformations to get all of the data ready for uploading to my database. 对于文件的每一行,我将该行中的数据存储到各种变量中,并进行一些转换以准备好上传到我的数据库的所有数据。 Then I plan to upload that row's data to the database with a PreparedStatement and INSERT query. 然后我计划使用PreparedStatement和INSERT查询将该行的数据上传到数据库。

I am unfamiliar with the best practices for my situation. 我不熟悉我的情况的最佳做法。 I'm in the process of teaching this to myself so I'm looking for a little guidance to make sure I do it correctly the first time. 我正在向自己教这个,所以我正在寻找一些指导,以确保我第一次正确地做到这一点。

Would it be inappropriate simply to open a connection at the beginning of the application's runtime (ie. open it once)? 仅仅在应用程序的运行时开始时打开一个连接是不合适的(即打开一次)? For each insert query I would create and close the PreparedStatement, but I would simply leave the connection open until I finished processing all of the files. 对于每个插入查询,我将创建并关闭PreparedStatement,但我只是将连接保持打开状态,直到我完成所有文件的处理。

I've read about connection pools elsewhere but I can't figure out if it is applicable in my situation. 我已经阅读过其他地方的连接池,但我无法弄清楚它是否适用于我的情况。 I know its expensive to open and close connections frequently, but shouldn't I be able to just open it once and run all of my queries under that connection? 我知道经常打开和关闭连接的成本很高,但我不应该只打开它一次并在该连接下运行我的所有查询?

If you would recommend using a connection pool, what service would you recommend for my situation. 如果您建议使用连接池,那么您会针对我的情况推荐哪种服务。 Preferably it will be easy to get a handle on quickly. 优选地,将很容易快速获得处理。 My project is somewhat time sensitive. 我的项目有些时间敏感。 Thanks. 谢谢。

If you are developing a desktop Java application, you won't be using connection pools. 如果您正在开发桌面Java应用程序,则不会使用连接池。 Connection pools are managed by Java Application Servers(Tomcat, JBoss, Glassfish) and will not be available in a desktop Java application. 连接池由Java应用程序服务器(Tomcat,JBoss,Glassfish)管理,在桌面Java应用程序中不可用。

Keeping a connection open is plausible if you are going to be doing just one update. 如果您要进行一次更新,保持连接打开是合理的。

Also, a batch update would be a great idea for you: 此外,批量更新对您来说是一个好主意:

String connectionURL = "jdbc:mysql://localhost:3306/database";
Connection con = null;
PreparedStatement stmt = null;

try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(connectionURL);
    stmt = con.prepareStatement(statement);

    for (Object o : list) {    // this is a list of your Java entity class
        stmt.setString(1, "foo");    // this is to update the parameters in the PreparedStatement
        stmt.setString(2, "bar");

         stmt.addBatch();     //  this adds the PreparedStatement to the batch of statements to execute
    }

    stmt.executeBatch();

} catch (SQLException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (stmt != null) {
        stmt.close();
    }
    if (con != null) {
        con.close();
    }
}

The idea of a batch update is to have one prepared statement, where you can simply change the parameters, and add the "new" statement to a batch of statements. 批量更新的想法是有一个预准备语句,您可以在其中简单地更改参数,并将“新”语句添加到一批语句中。 Then, you can execute them all at one go when you invoke stmt.executeBatch() 然后,当您调用stmt.executeBatch()时,您可以一次性执行它们

I would advise you however, to execute statements probably 30 at a time if you have many such statements. 但是,如果你有很多这样的陈述,我会建议你一次执行30个语句。 Otherwise, you would be stranded if the program crashes/fails. 否则,如果程序崩溃/失败,您将被搁浅。

This sounds like a batch proces, that you are implementing. 这听起来像是正在实施的批处理过程。 In that case a dedicated connection to the database is the better solution. 在这种情况下,与数据库的专用连接是更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM