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

后端 未结 6 2167
闹比i
闹比i 2020-11-30 00:01

I built an application and deployed locally ... and it was working perfectly. I deployed it on a remote server and started getting the exception mentioned in the subject lin

6条回答
  •  暖寄归人
    2020-11-30 00:30

    As @swanliu pointed out it is due to a bad connection.
    However before adjusting the server timing and client timeout , I would first try and use a better connection pooling strategy.

    Connection Pooling

    Hibernate itself admits that its connection pooling strategy is minimal

    Hibernate's own connection pooling algorithm is, however, quite rudimentary. It is intended to help you get started and is not intended for use in a production system, or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use c3p0.
    As stated in Reference : http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html

    I personally use C3P0. however there are other alternatives available including DBCP.
    Check out

    • http://www.mchange.com/projects/c3p0/index.html
    • http://commons.apache.org/dbcp/

    Below is a minimal configuration of C3P0 used in my application:

    org.hibernate.connection.C3P0ConnectionProvider
    1 
    100  
    100 
    0 
    10 
    1800  
    

    By default, pools will never expire Connections. If you wish Connections to be expired over time in order to maintain "freshness", set maxIdleTime and/or maxConnectionAge. maxIdleTime defines how many seconds a Connection should be permitted to go unused before being culled from the pool. maxConnectionAge forces the pool to cull any Connections that were acquired from the database more than the set number of seconds in the past.
    As stated in Reference : http://www.mchange.com/projects/c3p0/index.html#managing_pool_size

    Edit:
    I updated the configuration file (Reference), as I had just copy pasted the one for my project earlier. The timeout should ideally solve the problem, If that doesn't work for you there is an expensive solution which I think you could have a look at:

    Create a file “c3p0.properties” which must be in the root of the classpath (i.e. no way to override it for particular parts of the application). (Reference)

    # c3p0.properties
    c3p0.testConnectionOnCheckout=true
    

    With this configuration each connection is tested before being used. It however might affect the performance of the site.

提交回复
热议问题