Tomcatホット構成およびDataSourceの構成
TomcatはWebappsでプロジェクトを新規作成できますが、通常はプロジェクトファイルの管理に不利であり、バージョンの切り替えにも不利です.
Tomcatは、Tomcat FolderconfCatalinalocalhostディレクトリを構成する下でxxxを新規作成することができる.xmlはホット設定を行い、docBaseでプロジェクトファイルディレクトリを指すと
docBaseプロジェクトファイルが存在するディレクトリを指定
path Webアプリケーションパブリケーションの別名を指定する
reloadableプロジェクトファイルをリアルタイムで同期(パブリッシュ)するかどうかを指定
debugはdebugを許可するかどうかを指定します
(その他のパラメータは補足されます)
それ以外に、このファイルでDataSourceを構成することもできますが、Tomcat 5.0と5.5は少し違います.以下は5.5のデータソース構成です.
5.0のデータソース構成
Javaはコードを見てDataSourceを入手できます
db.properties
Tomcatは、Tomcat FolderconfCatalinalocalhostディレクトリを構成する下でxxxを新規作成することができる.xmlはホット設定を行い、docBaseでプロジェクトファイルディレクトリを指すと
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="E:\Work\AnsonSolder\WebRoot" path="/anson" reloadable="true" debug="0">
</Context>
docBaseプロジェクトファイルが存在するディレクトリを指定
path Webアプリケーションパブリケーションの別名を指定する
reloadableプロジェクトファイルをリアルタイムで同期(パブリッシュ)するかどうかを指定
debugはdebugを許可するかどうかを指定します
(その他のパラメータは補足されます)
それ以外に、このファイルでDataSourceを構成することもできますが、Tomcat 5.0と5.5は少し違います.以下は5.5のデータソース構成です.
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="E:\Work\AnsonSolder\WebRoot" path="/anson" reloadable="true" debug="0">
<Resource name="jdbc/AnsonSolderDB"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="sa"
password="dbsa"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@192.168.0.12:1521:test"/>
</Context>
5.0のデータソース構成
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="E:\Work\AnsonSolder\WebRoot" path="/anson" reloadable="true" debug="0">
<Resource name="jdbc/AnsonSolderDB"
auth="Container"
type="javax.sql.DataSource">
</Resource>
<ResourceParamters>
<Parameter>
<name>maxActive</name>
<value>100</value>
</Parameter>
<Parameter>
<name>maxIdle</name>
<value>30</value>
</Parameter>
<Parameter>
<name>maxWait</name>
<value>10000</value>
</Parameter>
<Parameter>
<name>username</name>
<value>sa</value>
</Parameter>
<Parameter>
<name>password</name>
<value>dbsa</value>
</Parameter>
<Parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</Parameter>
<Parameter>
<name>url</name>
<value>jdbc:oracle:thin:@192.168.0.12:1521:test</value>
</Parameter>
</ResourceParameters>
</Context>
Javaはコードを見てDataSourceを入手できます
import java.sql.*;
import java.io.*;
import java.util.*;
import java.util.Date;
import javax.naming.InitialContext;
import javax.sql.*;
public class DBConnectionManager {
private PrintWriter log;
private InitialContext context;
private DataSource myDS;
/**
* A private constructor since this is a Singleton
*/
public DBConnectionManager() {
init();
}
/**
* Returns a connection to the named pool.
*
* @param name The pool name as defined in the properties file
* @param con The Connection
*/
public synchronized void freeDBConnection(String name, DBConnection dbcon) {
try {
dbcon.close();
}
catch (Exception e) {
log(e, e.toString());
}
}
/**
* Returns an open connection. If no one is available, and the max
* number of connections has not been reached, a new connection is
* created.
*
* @param name The pool name as defined in the properties file
* @return Connection The connection or null
*/
public synchronized DBConnection getDBConnection(String name) {
try {
return new DBConnection(myDS.getConnection());
}
catch (Exception e) {
log(e, e.toString());
}
return null;
}
/**
* Loads properties and initializes the instance with its values.
*/
private void init() {
InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbProps = new Properties();
try {
dbProps.load(is);
}
catch (Exception e) {
System.err.println("Can't read the properties file. " +
"Make sure db.properties is in the CLASSPATH");
return;
}
String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log");
try {
log = new PrintWriter(new FileWriter(logFile, true), true);
}
catch (IOException e) {
System.err.println("Can't open the log file: " + logFile);
log = new PrintWriter(System.err);
}
String datasource = dbProps.getProperty("datasource", "jdbc/OracleCoreDS_ejms");
try {
context = new InitialContext();
myDS = (DataSource) context.lookup(datasource);
}
catch (Exception e) {
log(e, e.toString());
}
}
/**
* Writes a message to the log file.
*/
private void log(String msg) {
log.println(new Date() + ": " + msg);
}
/**
* Writes a message with an Exception to the log file.
*/
private void log(Throwable e, String msg) {
log.println(new Date() + ": " + msg);
e.printStackTrace(log);
}
}
db.properties
datasource=java:env/comp/jdbc/AnsonSolderDB