日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Tomcat中配置MySQL数据库连接池

發(fā)布時(shí)間:2025/7/14 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tomcat中配置MySQL数据库连接池 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Web開發(fā)中與數(shù)據(jù)庫的連接是必不可少的,而數(shù)據(jù)庫連接池技術(shù)很好的優(yōu)化了動態(tài)頁與數(shù)據(jù)庫的連接,相比單個(gè)連接數(shù)據(jù)庫連接池節(jié)省了很大的資源。用一個(gè)通俗的比喻:如果一個(gè)人洗澡需花一桶水,那一百個(gè)人就要花一百桶水,太浪費(fèi)了.如果都在池子里洗,洗多少個(gè)人都不怕了。

1.將MySQL的JDBC驅(qū)動復(fù)制到Tomcat安裝目錄里的lib文件夾下。驅(qū)動可以從MySQL官網(wǎng)上下載,為jar包。

2.將Tomcat的配置文件Context.xml做如下修改:

<Context path="/DBTest" docBase="DBTest"
??????? debug="5" reloadable="true" crossContext="true">

??? <!-- maxActive: 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 -1 for no limit.
???????? -->

??? <!-- maxIdle: 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.
???????? -->

??? <!-- maxWait: 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.
???????? -->

??? <!-- username and password: MySQL dB username and password for dB connections? -->

??? <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
???????? org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
???????? Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
???????? -->
???
??? <!-- url: 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.
???????? -->

? <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
?????????????? maxActive="100" maxIdle="30" maxWait="10000"
?????????????? username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
?????????????? url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

</Context>

注意代碼中紅色部分:DBTest 改為自己的項(xiàng)目路徑;TestDB改為自己的數(shù)據(jù)源名,但是后面使用時(shí)候要與這里的配置保持一致;javauser和 javauser改為自己MySQL的用戶名密碼;url的格式依次為jdbc:mysql://{你的數(shù)據(jù)庫服務(wù)所在的IP,如果為本機(jī)就為localhost}:{你的數(shù)據(jù)庫服務(wù)端口號}/{MySQL中要使用的數(shù)據(jù)庫名稱}?autoReconnect=true ?。

3.修改項(xiàng)目WEB-INF/web.xml 配置文件(若無,請新建),在“</web-app>”之上添加如下代碼:

? <resource-ref>
????? <description>DB Connection</description>
????? <res-ref-name>jdbc/TestDB</res-ref-name>
????? <res-type>javax.sql.DataSource</res-type>
????? <res-auth>Container</res-auth>
? </resource-ref>

上步中若修改了數(shù)據(jù)源名此步中紅色部分請保持與上步中的一致。

4.代碼示例:

Context initContext = new InitialContext();
Context envContext? = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
Statement st = null;
ResultSet rs = null;
st = conn.createStatement();
rs = st.executeQuery(yoursql);

注意紅色部分與上兩步中的一致;yoursql處寫你的sql代碼。

通過1-3步就在Tomcat中配置好了MySQL的數(shù)據(jù)庫連接池。

總結(jié)

以上是生活随笔為你收集整理的Tomcat中配置MySQL数据库连接池的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。