一個串連池的例子(說明)

來源:互聯網
上載者:User
這個串連池是直接從JIVE中取出來的,進行了一下修改,使得串連參數直接在程式中設定而不是從屬性檔案中讀取。

用法:
先設定自己的串連參數,在DbConnectionDefaultPool.java檔案的loadProperties方法中。注意你也需要設定串連池的log檔案的存放位置。

String driver="org.gjt.mm.mysql.Driver";//這是使用的JDBC驅動
String server="jdbc:mysql://192.100.100.1/qingtuo";//使用的URL
//String server="jdbc:mysql://192.168.0.1/qingtuo";
String username="qingtuo";
String password="qingtuo";
String minConnections="3";//最小串連數
String maxConnections="20";//最大串連數
String logPath="c:\\temp\\qingtuoDbLog.log";//記錄檔位置
//String logPath="/tmp/qingtuoDbLog.log";
String connectionTimeout="0.5";//定時清除無用串連間隔(以天為單位)

然後
在你的程式中只需要將這個包com.qingtuo.db.pool import進來,再向下面這樣用就行了。

    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs=null;
    try {
        con = DbConnectionManager.getConnection();
        //這裡寫你的SQL語句
    }
    catch (SQLException sqle) {
        throw sqle;
    }
    finally {
        try {
            pstmt.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        try {
            con.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

優點
雖然很多應用伺服器也有串連池,但這樣做的好處是,更加通用一些。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.