tomcat常用配置

[color=red]tomcat共享线程池的配置[/color]

配置很简单
第一步,打开共享的线程池


<Service name="Catalina">   
<!--The connectors can use a shared executor, you can define one or more named thread pools-->

<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="1000" minSpareThreads="50" maxIdleTime="600000"/>
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="1000" minSpareThreads="50" maxIdleTime="600000"/>

默认前后是注释<!-- -->掉的,去掉就可以了。其中

name
The name used to reference this pool in other places in server.xml. The name is required and must be unique.
这个是线程池的名字,必须唯一,我们在后面的配置里要用到这个东西

namePrefix
(String) The name prefix for each thread created by the executor. The thread name for an individual thread will be namePrefix+threadNumber
线程的名字前缀,用来标记线程名字的,这样每个线程就用这个前缀加上线程编号了,比如
catalina-exec-1
catalina-exec-2

maxThreads
(int) The max number of active threads in this pool, default is 200
允许的最大线程池里的线程数量,默认是200,大的并发应该设置的高一些,反正只是限制而已,不占用资源

minSpareThreads
(int) The minimum number of threads always kept alive, default is 25
最小的保持活跃的线程数量,默认是25.这个要根据负载情况自行调整了。太小了就影响反应速度,太大了白白占用资源。

maxIdleTime
(int) The number of milliseconds before an idle thread shutsdown, unless the number of active threads are less or equal to minSpareThreads. Default value is 60000(1 minute)
超过最小活跃线程数量的线程,如果空闲时间超过这个设置后,会被关别。默认是1分钟。

threadPriority
(int) The thread priority for threads in the executor, the default is Thread.NORM_PRIORITY
线程的等级。默认是Thread.NORM_PRIORITY


第二步
在 Connector里指定使用共享线程池


<Connector
port="8009"
protocol="AJP/1.3"
maxThreads="5000"
executor="tomcatThreadPool"
<Connector
port="8009"
protocol="AJP/1.3"
maxThreads="5000"
executor="tomcatThreadPool"

注意,一旦使用了线程池,则其它的线程属性,比如 maxThreads等将被忽略


我测试了一下,由于每次请求不再需要重新分配线程,系统响应速度还是有很明显的改善的。
[color=red]
连接池的配置[/color]

本方法的原理是,在%CATALINA%\conf\server.xml中设置数据库的连接属性,
在应用目录的/WEB-INF/web.xml中配置一个引用,
然后在应用中的/META-INF/context.xml中将以上两个配置联系起来。
所以真正产生连接的是tomcat系统级,因此数据库驱动应该放在%CATALINA%\common\lib中

以下是具体步骤:

1.将数据库驱动程序的JAR文件放在Tomcat的 common/lib 中。

2.在server.xml中设置数据源,以MySQL数据库为例,如下:
在<GlobalNamingResources> </GlobalNamingResources>节点中加入,
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password="root"
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://127.0.0.1:3306/test"
maxActive="4"/>
属性说明:name,数据源名称,通常取”jdbc/XXX”的格式;
type,”javax.sql.DataSource”;
password,数据库用户密码;
driveClassName,数据库驱动;
maxIdle,最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连
接将被标记为不可用,然后被释放。设为0表示无限制。
MaxActive,连接池的最大数据库连接数。设为0表示无限制。
maxWait ,最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示
无限制。

3.在你的web应用程序的web.xml中设置数据源参考,如下:
在<web-app></web-app>节点中加入,
<resource-ref>
<description>MySQL DB Connection Pool</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
子节点说明: description,描述信息;
res-ref-name,参考数据源名字,同上一步的属性name;
res-type,资源类型,”javax.sql.DataSource”;
res-auth,”Container”;
res-sharing-scope,”Shareable”;

4.在web应用程序的/META-INF/context.xml中设置数据源链接,如下:
在<Context></Context>中加入:
 <Context>
<ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"/>
</Context>
属性说明:name,同第2步和第3步的属性name值,和子节点res-ref-name值;
type,同样取”javax.sql.DataSource”;
global,同name值。

至此,设置完成,下面是如何使用数据库连接池。
1.建立一个连接池类,DBPool.java,用来创建连接池,代码如下:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBPool {
private static DataSource pool;
static {
Context env = null;
try {
env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource)env.lookup("jdbc/DBPool");
if(pool==null)
System.err.println("'DBPool' is an unknown DataSource");
} catch(NamingException ne) {
ne.printStackTrace();
}
}
public static DataSource getPool() {
return pool;
}
}


2.在要用到数据库操作的类或jsp页面中,用DBPool.getPool().getConnection(),
获得一个Connection对象,就可以进行数据库操作,最后别忘了对Connection对象调用close()方法,
注意:这里不会关闭这个Connection,而是将这个Connection放回数据库连接池。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值