mysql - Hibernate - Tomcat - MySQL 问题

标签 mysql hibernate tomcat struts c3p0

亲爱的大家,
我正在 tomcat7 上使用 hibernate-mysql 测试 Struts webApp...

8 小时超时后,我的 web 应用程序总是崩溃。我在这里和那里改变了配置。但没有成功。

非常感谢您的关注...

这里是来自 hibernate.xml 的一些行


 属性名称=“hibernate.bytecode.use_reflection_optimizer”>false
    属性名称=“hibernate.c3p0.idle_test_period”>30
    属性名称=“hibernate.c3p0.max_size”>600
    属性名称=“hibernate.c3p0.max_statements”>50
    属性名称=“hibernate.c3p0.min_size”>5
    属性名称=“hibernate.c3p0.timeout”>1800
    属性名称=“hibernate.c3p0.testConnectionOnCheckout”>true
    属性名称=“hibernate.connection.provider_class”>org.hibernate.connection.C3P0ConnectionProvider
    属性名称=“hibernate.c3p0.validate”>true
    属性名称=“hibernate.connection.driver_class”>com.mysql.jdbc.Driver
    属性名称=“hibernate.connection.url”>jdbc:mysql://localhost:3306/stockdb?autoReconnect=true


以下是我的堆栈跟踪中的一些行:



    com.mchange.v2.c3p0.impl.NewPooledConnection handleThrowable
    WARNING: [c3p0] A PooledConnection that has already signalled a Connection error is still in use!
    ...



WARNING: [c3p0] Another error has occurred [ com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed. ] which will not be reported to listeners!
...

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed.



...
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
...



at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.getMaxRows(NewProxyPreparedStatement.java:1200)




at org.hibernate.jdbc.AbstractBatcher.closeQueryStatement(AbstractBatcher.java:212)


...
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
...

请帮忙!

最佳答案

Tomcata database connection pool开箱即用。配置MySQL ,例如:

<Context>

    <!-- maxActive: Maximum number of database connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to -1 for no limit.
         -->

    <!-- maxIdle: Maximum number of idle database connections to retain in pool.
         Set to -1 for no limit.  See also the DBCP documentation on this
         and the minEvictableIdleTimeMillis configuration parameter.
         -->

    <!-- maxWait: Maximum time to wait for a database connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         -->

    <!-- username and password: MySQL username and password for database connections  -->

    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
         -->

    <!-- url: The JDBC connection url for connecting to your MySQL database.
         -->

  <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/javatest"/>

</Context>

然后您需要按如下方式配置连接:

    public static void main(String[] args) throws Exception {
        PoolProperties p = new PoolProperties();
        p.setUrl("jdbc:mysql://localhost:3306/mysql");
        p.setDriverClassName("com.mysql.jdbc.Driver");
        p.setUsername("root");
        p.setPassword("password");
        p.setJmxEnabled(true);
        p.setTestWhileIdle(false);
        p.setTestOnBorrow(true);
        p.setValidationQuery("SELECT 1");
        p.setTestOnReturn(false);
        p.setValidationInterval(30000);
        p.setTimeBetweenEvictionRunsMillis(30000);
        p.setMaxActive(100);
        p.setInitialSize(10);
        p.setMaxWait(10000);
        p.setRemoveAbandonedTimeout(60);
        p.setMinEvictableIdleTimeMillis(30000);
        p.setMinIdle(10);
        p.setLogAbandoned(true);
        p.setRemoveAbandoned(true);
 p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
        DataSource datasource = new DataSource();
        datasource.setPoolProperties(p); 

        Connection con = null;
        con = datasource.getConnection();

现在,像平常一样处理您的 JDBC 代码。

关于mysql - Hibernate - Tomcat - MySQL 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104195/

相关文章:

java - 由于指定的配置文件 [默认] 不匹配而跳过 XML bean 定义文件

java - 通讯链接失败 Caused by : java.net.SocketException : Connection reset.

javascript - 将带有 AJAX 的变量传递给 PHP 以填充下拉菜单时出现问题

Java Hibernate 注解(@JoinTable、@Filter)

php - 从mysql表中查找相关数据

java - 在 hibernate 中访问模式间表和关系

hibernate :集合删除子

java.net.SocketException : Software caused connection abort: recv failed; Causes and cures? 异常

mysql - 如何在sql中逐周显示

mysql排序没有外键的数据库表