Spring-Hibernate应用程序:非法访问:此Web应用程序实例已经被停止

11
我正在适当地处理连接。
1. 我在我的应用程序中使用'Hibernate'连接池。每当我从池中获取连接后,我会在事务完成后将其返回到池中。
2. 我监视数据库以检查连接。我设置了'空闲连接'时间为60秒。我发现没有连接对象运行超过60秒。

尽管如此,我经常遇到这个错误。我的Web应用程序也停止了。我不得不每天重启Tomcat一次。然而,我正在寻找一个永久的解决方案,而不需要重新启动Tomcat。
有人能解释一下根本原因吗?这样我就可以解决它。

错误日志:

INFO: Illegal access: this web application instance has been stopped already.  Could not load com.mchange.v2.c3p0.ComboPooledDataSourceBeanInfo.  The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
java.lang.IllegalStateException
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1587)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    ...
    ...
    ...
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:679)

我的 hibernate-contect.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            ">

    <context:property-placeholder location="/WEB-INF/spring.properties" />

    <!-- Enable annotation style of managing transactions -->
    <tx:annotation-driven transaction-manager="transactionManager" />   

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->                           
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                 p:dataSource-ref="dataSource"
                 p:configLocation="${hibernate.config}"
                 p:packagesToScan="com.turborep.turbotracker"/>

    <!-- Declare a datasource that has pooling capabilities-->   
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                destroy-method="close"
                p:driverClass="${app.jdbc.driverClassName}"
                p:jdbcUrl="${app.jdbc.url}"
                p:user="${app.jdbc.username}"
                p:password="${app.jdbc.password}"
                p:acquireIncrement="5"
                p:idleConnectionTestPeriod="60"
                p:maxPoolSize="100"
                p:maxStatements="50"
                p:minPoolSize="0" />

    <!-- Declare a transaction manager-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
                p:sessionFactory-ref="sessionFactory" />

</beans>

编辑1:
我今天又遇到了一个错误。以下是日志:

[ERROR] [ajp-bio-8009-exec-4 08:27:13] (JDBCExceptionReporter.java:logExceptions:234) Connections could not be acquired from the underlying database!
[ERROR] [ajp-bio-8009-exec-4 08:27:13] (JDBCExceptionReporter.java:logExceptions:234) Connections could not be acquired from the underlying database!
[ERROR] [ajp-bio-8009-exec-4 08:27:13] (JobServiceImpl.java:getRxmasterID:4399) Cannot open connection
org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
    ...
    ...
    ... 
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
    ... 50 more

示例代码,我如何使用连接:

@Resource(name="sessionFactory")
private SessionFactory itsSessionFactory;

@Override
public List<Userloginclone> getAllUserList() {
    itsLogger.debug("Retrieving all user list");
    Session aSession = null;
    List<Userloginclone> aQueryList = null;
    try {
        aSession = itsSessionFactory.openSession();
        Query aQuery = aSession.createQuery("FROM  Userloginclone");
        aQueryList = aQuery.list();
    } catch (Exception e) {
        itsLogger.error(e.getMessage(), e);
    } finally {
        aSession.close();
    }
    return  aQueryList;
}

请纠正我如果我做错了任何事情。

3个回答

7
您面临的问题与数据库连接无关。我猜当您重新启动tomcat并不重新部署应用程序时,您不会遇到此问题,对吗?
造成这种情况的原因是在tomcat中重新部署应用程序。每次重新部署应用程序都会创建一个新的网络应用程序实例,并停止旧的实例。可能在先前的应用程序中加载了某个旧类,或者该应用程序未正确卸载。
您在tomcat/lib中是否有一些jar文件? 能否展示JobQuoteFormController和JobServiceImpl类?

是的,如果我重新启动Tomcat,这个错误就不会出现。然而,2-3天后它又会再次出现。然后我需要再次重启Tomcat。 - vissu
最近2-3天是否有重新部署?你能否检查一下日志,看看Tomcat是否因某些原因没有自动重新部署应用程序? - František Hartman
这2-3天内没有重新部署。每次它崩溃时,我都会重新启动Tomcat。2-3天后,它又会崩溃。 - vissu
在我的情况下,它在20分钟后崩溃。重启也没有帮助。 - t_sologub

5
首先,您可能希望使用Spring的事务管理器和Hibernate的OpenSessionInViewFilter来管理Session和事务处理。有了这两个工具,您就不必担心管理连接,并可以通过itsSessionFactory.getCurrentSession()获取一个Session。
接下来,您应该在dataSource bean中添加p:testConnectionOnCheckout="true"
您的应用程序正在发生的情况是一旦连接失效,它就会一直保持失效状态。这将允许重新启动失效的连接。

你的回答没有解决我的问题。不过,我得到了一些修复它的想法。 - vissu
@vissu,如果你解决了这个问题,能否分享一下你的解决方案? - Halil
@Halil May,我没有修复这个问题,现在也不在那个项目上工作了。我让它没有修复。 :( - vissu

1
  1. service tomcat stop

  2. ps -ef|grep java # 查看Java进程并杀死它们

  3. service tomcat start # 然后它就可以工作了

PS:您可以在 tomcat/logs/catalina.outlocalhost-2018....log 中查看日志


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接