Tomcat 热配置以及配置DataSource

Tomcat可以在Webapps新建项目,但通常这样的做法不利于管理项目文件,也不利于切换版本

Tomcat可以在配置Tomcat Folder\conf\Catalina\localhost\目录下新建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 application发布的别名

reloadable指定是否实时同步(发布)项目文件

debug指定是否允许debug

(其他参数待补充)

 

除此之外,还可以在这个文件配置DataSource,但是Tomcat5.0跟5.5有点区别,以下是5.5的DataSource配置

<?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的DataSource配置

<?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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值