Tomcat7 使用 DataSource(MySQL)

之前比較好命有 JBoss 可以用,現在只有 Tomcat ,有些預設沒開的功能就要想辦法整合進來了,這陣子弄了些有的沒的,趕快記錄一下,免得忘記。

1) 在 Tomcat 下設定

到 $tomcat安裝路徑/conf/context.xml 新增 DataSource 的設定。

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--><!-- The contents of this file will be loaded for each web application --><Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<!--
Data source of MAD database.
maxActive: Connection Pool 同時所能建立的連線最大數量。若為負值,則無上限。
maxIdle: Connection Pool 同時所能允許閒置的連線最大數量。若為負值,則無上限。
maxWait: 等待可用 Connect 的 ms 數,超過時間就丟出例外。
removeAbandonedTimeout: 超過閒置秒數的連線會被移除。
dbcp config: http://commons.apache.org/proper/commons-dbcp/configuration.html
http://simon-tech.blogspot.tw/2012/02/tomcat-datasource.html
-->
<Resource name="jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
testWhileIdle="true"
testOnBorrow="true"
testOnReturn="false"
validationQuery="SELECT 1"
validationInterval="30000"
timeBetweenEvictionRunsMillis="30000"
maxActive="5"
maxIDle="4"
minIDle="4"
maxWait="10000"
initialSize="2"
removeAbandonedTimeout="300"
removeAbandoned="true"
logAbandoned="true"
minEvictableIdleTimeMillis="30000"
jmxEnabled="true"
jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;
org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
username="${mysql_user}"
password="${mysql_password}"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://${mysql_server_ip}:3306/${database_name}"
connectionProperties="useUnicode=true;autoReconnect=true;characterEncoding=utf-8;"/>
</Context>

2) 記得下載 mysql-connector.jar 放到 $tomcat安裝路徑/lib 底下。

3) 重新啟動 Tomcat 就應該可以看到有無連上 MySQL 。

4) 程式碼就可以透過 JNDI 用 jdbc/TestDB (上面設定檔替 DataSource 取的名稱)拿到 DataSource。

Eclipse 整合 Tomcat 遇到問題

但是從 Eclipse 下直接啟動 Tomcat 就會發現一直拿不到 DaraSource,是因為 Eclipse 開啓 Tomcat 時,會用預設的設定檔去覆蓋 Tomcat 的設定檔造成的。

其中一個解法是,直接修改 $eclipse使用的workspace/Servers/Tomcat/context.xml,把上一段做的變動改寫過來即可。

但這樣 Tomcat 和 Eclipse 會有兩個 context.xml (Don't Repeat Yourself),而用 Eclipse 啟動又會覆寫 Tomcat 原本的 context.xml (容易讓人搞不清楚到底以哪個為準)。所以就用了下面的做法。

綁定專案設定

直接修改 $project路徑/WebContent/META-INF/context.xml,把上一段做的變動改寫過來即可。Tomcat 再載入專案時就會加上專案裡新增的 DataSource。

這樣做之後接手的人,只需要部署 war 檔,不用特別到 Tomcat 下做設定,比較符合我個人開發專案的習慣。

0 則回應: