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

后端 未结 6 2142
闹比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:11
    1. First Replace the MySQL dependency as given below

      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.44</version>
      </dependency>
      
    2. An error showing "Authentication plugin 'caching_sha2_password'" will appear. Run this command:

      mysql -u root -p
      ALTER USER 'username'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
      
    0 讨论(0)
  • 2020-11-30 00:14

    If you don't want use connection pool (you sure, that your app has only one connection), you can do this - if connection falls you must establish new one - call method .openSession() instead .getCurrentSession()

    For example:

    SessionFactory sf = null;
    // get session factory
    // ...
    //
    Session session = null;
    try {
            session = sessionFactory.getCurrentSession();
    } catch (HibernateException ex) {
            session = sessionFactory.openSession();
    }
    

    If you use Mysql, you can set autoReconnect property:

        <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1/database?autoReconnect=true</property>
    

    I hope this helps.

    0 讨论(0)
  • 2020-11-30 00:21

    Please make sure you are using latest jdbc connector as per the mysql. I was facing this problem and when I replaced my old jdbc connector with the latest one, the problem was solved.

    You can download latest jdbc driver from https://dev.mysql.com/downloads/connector/j/

    Select Operating System as Platform Independent. It will show you two options. One as tar and one as zip. Download the zip and extract it to get the jar file and replace it with your old connector.

    This is not only for hibernate framework, it can be used with any platform which requires a jdbc connector.

    0 讨论(0)
  • 2020-11-30 00:26

    This is due to using obsolete mysql-connection-java version, your MySQl is updated but not your MySQL jdbc Driver, you can update your connection jar from the official site Official MySQL Connector site. Good Luck.

    0 讨论(0)
  • 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:

    <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    <property name="c3p0.acquire_increment">1</property> 
    <property name="c3p0.idle_test_period">100</property> <!-- seconds --> 
    <property name="c3p0.max_size">100</property> 
    <property name="c3p0.max_statements">0</property> 
    <property name="c3p0.min_size">10</property> 
    <property name="c3p0.timeout">1800</property> <!-- seconds --> 
    

    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.

    0 讨论(0)
  • 2020-11-30 00:34

    MySQL implicitly closed the database connection because the connection has been inactive for too long (34,247,052 milliseconds ≈ 9.5 hours). If your program then fetches a bad connection from the connection-pool that causes the MySQLNonTransientConnectionException: No operations allowed after connection closed.

    MySQL suggests:

    You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property autoReconnect=true to avoid this problem.

    0 讨论(0)
提交回复
热议问题