سؤال

بعد الحصول على سئم من قفل C3P0 المستمر ، أنتقل إلى Bonecp للحصول على مجموعة اتصال بديلة لقاعدة البيانات الخاصة بي. لديّ تطبيق خادم يعالج حوالي 7000 عنصر في الدقيقة ويحتاج إلى تسجيل هذه العناصر في قاعدة بيانات MySQL الخاصة بنا. لدي حاليًا 100 موضوع عامل وقمت بإنشاء حمام السباحة الخاص بي على هذا النحو:

BoneCPConfig config = new BoneCPConfig();
      config.setJdbcUrl("jdbc:mysql://"+Settings.MYSQL_HOSTNAME+"/"+Settings.MYSQL_DATABASE+"?autoReconnectForPools=true" ); 
      config.setUsername(Settings.MYSQL_USERNAME); 
      config.setPassword(Settings.MYSQL_PASSWORD);
      config.setMinConnectionsPerPartition(5);
      config.setMaxConnectionsPerPartition(10);
      config.setPartitionCount(5);
      config.setAcquireIncrement(5);
      connectionPool = new BoneCP(config); // setup the connection pool

هل تلك الإعدادات المقبولة لمثل هذا التطبيق؟ أنا أسأل لأنه بعد دقيقة أو دقيقتين من الجري ، كنت أحصل على استثناءات bonecp عند محاولة الاتصال getConnection على المسبح. شكرا للمساعدة.

فيما يلي الرمز الذي كنت أستخدمه لمكالمات DB في موضوعات العمال الخاصة بي ، لا يمكن أن يفشل في dbConn = this.dbPool.getConnection() خط. هل أنا لا أغلق الاتصالات بشكل صحيح؟

private void insertIntoDb() {
    try {  
        Connection dbConn = this.dbPool.getConnection();

        try {
            PreparedStatement ps3 = dbConn.prepareStatement("INSERT IGNORE INTO test_table1 SET test1=?, test2=?, test3=?");
            ps3.setString(1, "some string");
            ps3.setString(2, "some other string");
            ps3.setString(3, "more strings");
            ps3.execute();
            ps3.close();

            PreparedStatement ps4 = dbConn.prepareStatement("INSERT IGNORE INTO test_table2 SET test1=?, test2=?, test3=?");
            ps4.setString(1, "some string");
            ps4.setString(2, "some other string");
            ps4.setString(3, "more strings");
            ps4.execute();
            ps4.close();

        } catch(SQLException e) {
            logger.error(e.getMessage());
        } finally {
            try {
                dbConn.close();
            } catch (SQLException e) {
                logger.error(e.getMessage());
            }
        }
    } catch(SQLException e) {
        logger.error(e.getMessage());
    }
}

هذا هو الخطأ الذي كنت أراه:

 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.
 [java]  WARN [com.google.common.base.internal.Finalizer] (ConnectionPartition.java:141) - BoneCP detected an unclosed connection and will now attempt to close it for you. You should be closing this connection in your application - enable connectionWatch for additional debugging assistance.

ERROR pool-2-thread-39 2010-09-04 13:36:19,798 com.test.testpackage.MyTask - null
java.sql.SQLException
    at com.jolbox.bonecp.BoneCP.getConnection(BoneCP.java:381)
هل كانت مفيدة؟

المحلول

هل تلك الإعدادات المقبولة لمثل هذا التطبيق؟ أنا أسأل لأنه بعد دقيقة أو دقيقتين من الركض ، كنت أحصل على استثناءات bonecp عند محاولة الاتصال بـ getConnection على المسبح. شكرا للمساعدة.

إذا كان لديك 100 عامل ، فلماذا تقتصر على التجمع على 50 اتصالًا (عدد الأقسام x max عدد الاتصالات لكل قسم ، أي 5 × 10 في قضيتك)؟

هل أنا لا أغلق الاتصالات بشكل صحيح؟

تبدو جيدة (ولكن ربما تمكين connectionWatch كما ألمح لمعرفة ماهية التحذير بالضبط). أنا شخصياً أغلق جميع الموارد التي أستخدمها ، بما في ذلك مجموعات البيان والنتائج. فقط في حالة ، إليك المصطلح الذي أستخدمه:

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
    conn = pool.getConnection();
    pstmt = conn.prepareStatement(SOME_SQL);
    pstmt.setFoo(1, foo);
    ...
    rs = pstmt.executeQuery();
    ...
} finally {
    if (rs != null) try { rs.close(); } catch (SQLException quiet) {}
    if (pstmt != null) try { pstmt.close(); } catch (SQLException quiet) {}
    if (conn != null) try { conn.close(); } catch (SQLException quiet) {}
}

يمكنك تجميع المكالمات أعلاه بطريقة ثابتة لفئة الأداة المساعدة.

أو يمكنك استخدامها DbUnit.closeQuietly(Connection, Statement, ResultSet) من المشاعات dbutils التي تفعل هذا بالفعل.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top