2013-02-27 113 views
2

親愛的,
我使用休眠,MySQL在tomcat7測試 Struts的Web應用程序...休眠 - Tomcat的 - MySQL的問題

的8小時超時時間我總是Web應用程序崩潰 。我在這裏和那裏改變了配置。但沒有成功。

我真的很感謝您的關注...

這裏從 hibernate.xml一些線條


property name="hibernate.bytecode.use_reflection_optimizer">false 
    property name="hibernate.c3p0.idle_test_period">30 
    property name="hibernate.c3p0.max_size">600 
    property name="hibernate.c3p0.max_statements">50 
    property name="hibernate.c3p0.min_size">5 
    property name="hibernate.c3p0.timeout">1800 
    property name="hibernate.c3p0.testConnectionOnCheckout">true 
    property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider 
    property name="hibernate.c3p0.validate">true 
    property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver 
    property name="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) 
... 

請幫幫忙!

+0

Tomcat具有[內置數據庫連接池](http://people.apache.org/~fhanik/tomcat/jdbc-pool.html),無需渲染c3p0。 – hd1 2013-02-27 04:27:12

+0

真棒。這對我的可憐知識來說是全新的......非常感謝你的提示。 – shan 2013-02-27 04:32:25

+0

我會把它作爲一個答案,以及從頁面的一些配置細節,所以你可以在第二個 – hd1 2013-02-27 04:33:35

回答

2

Tomcat已將a 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代碼。

+0

請告訴我這個... 我該如何結合hibernate和struts ...會是衝突嗎? – shan 2013-02-27 04:50:03

+0

考慮到[最受尊敬的Java博客之一](http://www.mkyong.com/struts/struts-hibernate-integration-example/)有一個關於這個的教程,我猜測它不會是一個衝突。 – hd1 2013-02-27 04:52:39

+0

再次感謝...這是工作。你是救生員。 – shan 2013-02-27 06:51:29