在JBOSS中使用mysql連線池

season0891發表於2009-07-02
轉自 網路

連線池配置:
從${JBOSS_HOME}/docs/examples/jca目錄下把mysql-ds.xml複製到${JBOSS_HOME}/server/default/deploy下,做如下少許修改



<!-- $Id: mysql-ds.xml 63175 2007-05-21 16:26:06Z rrajesh $ --&gt
<!-- Datasource config for MySQL using 3.0.9 available from:

--&gt


 
    mysqlds
    jdbc:mysql://localhost:3306/discuz
    com.mysql.jdbc.Driver
    root
    123456
    org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
    <!-- should only be used on drivers after 3.22.1 with "ping" support
    org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker
    --&gt
    <!-- sql to call when connection is created
    some arbitrary sql
      --&gt
    <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers
    some arbitrary sql
      --&gt

    <!--最小連線池數目--&gt
    5

    <!--最大連線池數目--&gt
    800

    <!-- 資料庫連線空閒時間,單位為分鐘,如果負載較大,可以設為5,如果一般,可以設為3 --&gt
    5

    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml --&gt
   
       mysql
   

 



JBoss測試JSP :

         import="java.io.*,java.util.*,java.sql.*,javax.sql.*,javax.naming.*"%>
        

   
        <br>            jboss 連線池測試<br>       
   
   
       


            test
           

            連線池:
       


                    try {
                Context ctx = new javax.naming.InitialContext();
                DataSource ds = (DataSource) ctx.lookup("java:mysqlds");


                Connection conn = ds.getConnection();
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("select * from cdb_members");
                while (rs.next()) {
                    out.print("
\n" + rs.getString(2));

                }
                out.print("
Successful!成功!!\n");
                rs.close();
                stmt.close();
                conn.close();
            } catch (Exception e) {
                out.print("出現意外!" + e.getMessage());
                e.printStackTrace();
            }
       
        %>
       
   

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-608121/,如需轉載,請註明出處,否則將追究法律責任。

相關文章