首页 > 网站 > Tomcat > 正文

解决tomcat频繁死掉的问题

2024-09-06 19:01:05
字体:
来源:转载
供稿:网友

某天在服务器上的网页打不开了,频繁报以下错误。

2007-3-18 1:08:26 org.apache.tomcat.util.threads.threadpool logfull
严重: all threads (150) are currently busy, waiting. increase maxthreads (150) or check the servlet status

在网上找了些回答,以下是我觉得正确的回答:
1.我想你的部分资源没有释放,积压卡死的
2.连接池问题
3.应该是服务器端响应request的线程的处理时间过长导致的

分析:
当时使用网站的人数只有2个人,不可能答到到了并发线程150的上线。所以应该不是数据库的问题。
通过对出错的提示判断,应该是连接池使用不合理造成的,或者根本没设置连接池。和数据库连接的部分是使用spring的数据源jdbc连的,如下:
<beans>
    <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource">
        <!-- driver for mysql-->
        <property name="driverclassname"><value>org.gjt.mm.mysql.driver</value></property>
        <property name="url"><value>jdbc:mysql://localhost:3306/test?useunicode=true&amp;characterencoding=utf8</value></property>
        <property name="username"><value>test</value></property>
        <property name="password"><value>test</value></property>      
</beans>

问题应该出现在spring的drivermanagerdatasource上,它负责管理这些连接的。
下边是对drivermanagerdatasource 的解释
drivermanagerdatasource in spring framework

   javax.sql interface datasource

implementation of smartdatasource that configures a plain old jdbc driver via
bean properties, and returns a new connection every time.

useful for test or standalone environments outside of a j2ee container, either
as a datasource bean in a respective applicationcontext, or in conjunction with
a simple jndi environment. pool-assuming connection.close() calls will simply
close the connection, so any datasource-aware persistence code should work.

in a j2ee container, it is recommended to use a jndi datasource provided by the
container. such a datasource can be exported as a datasource bean in an
applicationcontext via jndiobjectfactorybean, for seamless switching to and from
a local datasource bean like this class.

if you need a "real" connection pool outside of a j2ee container, consider
apache's jakarta commons dbcp. its basicdatasource is a full connection pool
bean, supporting the same basic properties as this class plus specific settings.
it can be used as a replacement for an instance of this class just by changing
the class name of the bean definition to
"org.apache.commons.dbcp.basicdatasource".

-----------------------------------------------
many jakarta projects support interaction with a relational database. creating a
new connection for each user can be time consuming (often requiring multiple
seconds of clock time), in order to perform a database transaction that might
take milliseconds. opening a connection per user can be unfeasible in a
publicly-hosted internet application where the number of simultaneous users can
be very large. accordingly, developers often wish to share a "pool" of open
connections between all of the application's current users. the number of users
actually performing a request at any given time is usually a very small
percentage of the total number of active users, and during request processing is
the only time that a database connection is required. the application itself
logs into the dbms, and handles any user account issues internally.

there are several database connection pools already available, both within
jakarta products and elsewhere. this commons package provides an opportunity to
coordinate the efforts required to create and maintain an efficient,
feature-rich package under the asf license.

the commons-dbcp package relies on code in the commons-pool package to provide
the underlying object pool mechanisms that it utilizes.

applications can use the commons-dbcp component directly or through the existing
interface of their container / supporting framework. for example the tomcat
servlet container presents a dbcp datasource as a jndi datasource. james (java
apache mail enterprise server) has integrated dbcp into the avalon framework. a
avalon-style datasource is created by wrapping the dbcp implementation. the
pooling logic of dbcp and the configuration found in avalon's excalibur code is
what was needed to create an integrated reliable datasource.

看完了解释,事实上是因为drivermanagerdatasource建立连接是只要有连接就新建一个connection,根本没有连接池的作用。改为以下开源的连接池会好点。
<bean id="mydatasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close">
<property name="driverclassname">
<value>org.hsqldb.jdbcdriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost:9001</value>
</property>
<property name="username">
<value>sa</value>
</property>
<property name="password">
<value></value>
</property>
</bean>

测试通过,问题消除,如果没有搜索引擎找答案不会这么快解决问题。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表