避免 JDBC 调用

发布于 2024-11-07 09:04:43 字数 1730 浏览 0 评论 0原文

场景是这样的:

for loop // runs say 200000 times
{
    // here, i do a select from a database, fetching few rows which are expected to increase with every new iteration of for loop
    // currently i am doing this select using simple JDBC call (using JDBC only is NOT a requirement)

    // then i do some string matching stuff and then i either insert or update a particular row (in 95% cases i will insert)
    // this insert or update is being done using Hibernate (using Hibernate over here is a requirement)
}

所以问题是,在每个循环中,我必须考虑每个先前插入/更新的行。由于这个要求,我必须在每个循环中执行 JDBC 调用。而且这个 JDBC 调用花费了最长的时间,从而降低了性能。

我想知道,是否有任何方法可以让我不必在每次迭代中进行 JDBC 调用,但我仍然能够考虑所有记录,包括前一个插入/更新中的记录?诸如缓存或内存数据结构之类的东西吗?

这是代码:

for loop // runs say 2000 times
{
    String query = pdi.selectAllPatients(patientInfo);
    Statement st = conn.createStatement();
    ResultSet patientRs = st.executeQuery(query);

    while (patientRs.hasNext())
    {
        // some string ops
    }

    // Create session for DB No.2
    Session sessionEmpi = sessionFactoryEmpi.getCurrentSession();
    sessionEmpi.beginTransaction();

    if(some condition)
        patientDao.insertPatient(patientInfo, sessionEmpi);
    else
        patientDao.insertref(patientInfo.getref(), sessionEmpi);

    conn.commit();
}

public int insertPatient(PatientInfo input, Session session) throws SQLException {

    try {

        session.save(input.getPatient());
        session.flush();
        session.save(input.getref());
        session.getTransaction().commit();

        return 1;

    } catch (Exception ex) {
        session.getTransaction().rollback();
        ex.printStackTrace();
        return 0;
    }
}

The scenario is like this:

for loop // runs say 200000 times
{
    // here, i do a select from a database, fetching few rows which are expected to increase with every new iteration of for loop
    // currently i am doing this select using simple JDBC call (using JDBC only is NOT a requirement)

    // then i do some string matching stuff and then i either insert or update a particular row (in 95% cases i will insert)
    // this insert or update is being done using Hibernate (using Hibernate over here is a requirement)
}

So the problem is, in every loop, I have to consider the each and every previously inserted/updated row. Due to this requirement, I have to do a JDBC call in each and every loop. And this JDBC call is taking the maximum time, bringing down the performance.

I want to know, is there any method using which I do not have to make a JDBC call in each iteration, but still I will be able to consider all the records including the one in the just previous insert/update? Anything like caching or some in-memory data structure or something like that?

Here is the code:

for loop // runs say 2000 times
{
    String query = pdi.selectAllPatients(patientInfo);
    Statement st = conn.createStatement();
    ResultSet patientRs = st.executeQuery(query);

    while (patientRs.hasNext())
    {
        // some string ops
    }

    // Create session for DB No.2
    Session sessionEmpi = sessionFactoryEmpi.getCurrentSession();
    sessionEmpi.beginTransaction();

    if(some condition)
        patientDao.insertPatient(patientInfo, sessionEmpi);
    else
        patientDao.insertref(patientInfo.getref(), sessionEmpi);

    conn.commit();
}

public int insertPatient(PatientInfo input, Session session) throws SQLException {

    try {

        session.save(input.getPatient());
        session.flush();
        session.save(input.getref());
        session.getTransaction().commit();

        return 1;

    } catch (Exception ex) {
        session.getTransaction().rollback();
        ex.printStackTrace();
        return 0;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生一梦 2024-11-14 09:04:43

SELECT 的性能是否一致?除非您的数据相当小,否则您可能无法在内存中缓存所有更改。您还可以对 SELECT 进行批处理,从而有效地展开循环。

Is the performance of the SELECT consistent? Unless your data is fairly small, you'll likely have trouble caching all your changes in memory. You may also be able to batch the SELECTs, effectively unrolling the loop.

水波映月 2024-11-14 09:04:43

您可以使用PreparedStatement接口而不是Statement接口,因为它避免了不必要的调用来触发对数据库的查询,您只需将数据绑定到for循环中,这将帮助您提高性能!

例子:

PreparedStatement s =con.prepareStatement("select * from student_master where stu_id = ?");

for()
{
   s.setString(1,"s002");
   ResultSet rs = s.executeQuery();
}

You can use the PreparedStatement interface instead of Statement interface as it avoids the unnecessary calls for firing the query to the database you just have to bind the data in for loop this will help you to improve performance!!

example:

PreparedStatement s =con.prepareStatement("select * from student_master where stu_id = ?");

for()
{
   s.setString(1,"s002");
   ResultSet rs = s.executeQuery();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文