Tomcat配置SQLServer串連池

來源:互聯網
上載者:User
server|sqlserver 1.       進入tomcat目錄下的conf檔案夾目錄裡,找到Server.xml在tomcat的Server.xml檔案中在</Context>標籤與</Host>標籤裡,定議串連語句,格式如下

<!—path定議你的應用程式的目錄所 ,/DBTest表示在Tomcat Webapps目錄下à

<!—docBase=”DBTest” 表示你執行止程式時路徑名稱,例如以下的執行路徑就是http://localhost:8080/DBTest--> <Context path="/DBTest" docBase="DBTest"        debug="5" reloadable="true" crossContext="true">

    <!-- maxActive: 串連池的最大數量,要確保有足夠的串連數-->

    <!-- maxIdle: 最大空閑串連數,設定為-1即表示不限制-->

    <!-- maxWait:最長等待連線時間(最大等待串連池反回可用的時間), 以納秒為單位,即設為10000相等於10秒,如果設定成-1表示不確定-->

    <!-- username and password: 串連資料庫使用的帳號與密碼  -->

    <!-- driverClassName:串連資料庫的驅動程式,如SQLServer就是

com.microsoft.jdbc.sqlserver.SQLServerDriver.-->

    <!-- url: 串連資料庫路徑,如

jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=KB_Rate-->

     <Resource name=" Default_JDBC " auth="Container" type="javax.sql.DataSource"               maxActive="100" maxIdle="30" maxWait="10000"               username="javauser" password="javadude" driverClassName=" com.microsoft.jdbc.sqlserver.SQLServerDriver "            url=" jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=KB_Rate""/>

   </Context>

   配置完成後,在DBTest目錄下添加一個Test檔案,如下:<%@ page contentType="text/html;charset=gb2312"%><%@ page import="java.sql.*"%><%@ page import="javax.sql.*"%><%@ page import="javax.naming.*"%><%  Connection conn = null;  Context initCtx = new InitialContext();  if (initCtx == null)     throw new Exception("不能擷取Context!");      Context ctx = (Context) initCtx.lookup("java:comp/env");       Object obj = (Object) ctx.lookup("/Default_JDBC ");//擷取串連池對象      javax.sql.DataSource ds = (javax.sql.DataSource) obj; //類型轉換      conn = ds.getConnection();  Statement stmt = conn.createStatement();  PreparedStatement ps=conn.prepareStatement("select * from FinalOrderdata");  ResultSet rs=ps.executeQuery();  while(rs.next()){      out.println(rs.getString(1)+"<BR>");        i++;  }rs.close();stmt.close();conn.close(); out.println("串連池測試成功"+i);

   到於TOMCAT5.0版本的設定也是差不多,不過在Server.xml中添加串連池設定語句上有所有不,可參考http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html其它操作也是差不多,<Context path="/DBTest" docBase="DBTest"        debug="5" reloadable="true" crossContext="true"><Context path="/DBTest" docBase="DBTest"        debug="5" reloadable="true" crossContext="true">  <Logger className="org.apache.catalina.logger.FileLogger"             prefix="localhost_DBTest_log." suffix=".txt"             timestamp="true"/>  <Resource name="jdbc/TestDB"               auth="Container"               type="javax.sql.DataSource"/>  <ResourceParams name="jdbc/TestDB">    <parameter>      <name>factory</name>      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>    </parameter>    <!-- Maximum number of dB connections in pool. Make sure you         configure your mysqld max_connections large enough to handle         all of your db connections. Set to 0 for no limit.         -->    <parameter>      <name>maxActive</name>      <value>100</value>    </parameter>    <!-- Maximum number of idle dB connections to retain in pool.         Set to -1 for no limit.  See also the DBCP documentation on this         and the minEvictableIdleTimeMillis configuration parameter.         -->    <parameter>      <name>maxIdle</name>      <value>30</value>    </parameter>    <!-- Maximum time to wait for a dB connection to become available         in ms, in this example 10 seconds. An Exception is thrown if         this timeout is exceeded.  Set to -1 to wait indefinitely.         -->    <parameter>      <name>maxWait</name>      <value>10000</value>    </parameter>    <!-- MySQL dB username and password for dB connections  -->    <parameter>     <name>username</name>     <value>javauser</value>    </parameter>    <parameter>     <name>password</name>     <value>javadude</value>    </parameter>    <!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next         if you want to use this driver - we recommend using Connector/J though    <parameter>       <name>driverClassName</name>       <value>org.gjt.mm.mysql.Driver</value>    </parameter>     -->    <!-- Class name for the official MySQL Connector/J driver -->    <parameter>       <name>driverClassName</name>       <value>com.mysql.jdbc.Driver</value>    </parameter>    <!-- The JDBC connection url for connecting to your MySQL dB.         The autoReconnect=true argument to the url makes sure that the         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the         connection.  mysqld by default closes idle connections after 8 hours.         -->    <parameter>      <name>url</name>      <value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>    </parameter>  </ResourceParams></Context>有不足的地方,請各為指正!

   還有,的就是請大蝦門可以告訴一下小弟,如何設定法才能讓串連池做到最高效,因為我試過用TOMCAT串連池連池,發現比較慢,反應較久,我不知道設錯了那裡,,!!我的設定如下:<Context path="/KB_Rate" docBase="/KB_Rate"        debug="5" reloadable="true" crossContext="true">     <Resource name="Default_JDBC" auth="Container" type="javax.sql.DataSource"               maxActive="100" maxIdle="30" maxWait="10000"               username="sa" password="1234" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"              url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=KB_Rate"/></Context>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.