WARN SqlExceptionHelper:143 - SQL Error: 0, SQLState: 08S01- SqlExceptionHelper:144 - Communications link failure

后端 未结 2 1076
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 03:57

I have problem with Hibernate (hibernate-core-4.1.9.Final.jar)

Case 1: Hibernate testing inside for loop. Everything went well.

for(in         


        
相关标签:
2条回答
  • 2020-12-18 04:11

    The problem was happening because of the small value for time_out variable on the MySQL server.

    In my situation, time_out was set to 1 minute. Using C3PO pooling mechanism we can optimize JDBC.

    Download c3p0 -> http://sourceforge.net/projects/c3p0/

    I'm using hibernate 3.0.

    hibernate.cfg.xml

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     
    <hibernate-configuration>
     
        <session-factory>
           
             <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
              <property name="connection.url">jdbc:mysql://databasehost:3306/databasename</property>
              <property name="connection.username">user</property>
              <property name="connection.password">psw</property>
              <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
              <property name="hibernate.hbm2ddl.auto">update</property>
             
              <property name="show_sql">false</property>
             
           <!-- Hibernate c3p0 settings-->
            <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
            <property name="hibernate.c3p0.acquire_increment">3</property>
            <property name="hibernate.c3p0.idle_test_period">10</property>
            <property name="hibernate.c3p0.min_size">5</property>
            <property name="hibernate.c3p0.max_size">75</property>
            <property name="hibernate.c3p0.max_statements">10</property>
            <property name="hibernate.c3p0.timeout">50</property>
            <property name="hibernate.c3p0.preferredTestQuery">select 1</property>
            <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
              
             <!-- Mapping files -->
            <mapping class="xxx.xxx.xxx.xxx" />
            <mapping class="xxx.xxx.xxx.xxx" />
            <mapping class="xxx.xxx.xxx.xxx" />
            <mapping class="xxx.xxx.xxx.xxx" />
            
           
        </session-factory>
    </hibernate-configuration> 
    

    PersistenceManager.java

    import java.io.PrintStream;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.cfg.Configuration;
    
    public class PersistenceManager
    {
      private static SessionFactory sessionFactory = null;
    
      private static PersistenceManager singleton = null;
    
      public static PersistenceManager getInstance()
      {
        if (singleton == null)
        {
          singleton = new PersistenceManager();
        }
    
        return singleton;
      }
    
      public SessionFactory getSessionFactory()
      {
        if (sessionFactory == null)
          createSessionFactory();
        return sessionFactory;
      }
    
      protected void createSessionFactory()
      {    
        sessionFactory = new AnnotationConfiguration().configure()
          .buildSessionFactory();
      }
    
      public void destroySessionFactory()
      {
        if (sessionFactory != null)
        {
          sessionFactory.close();
          sessionFactory = null;
        }
      }
    }
    

    Example 1:

    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;
    
    public Users Login( String username,  String password)
      {
        Session session = null;
        try
        {
          String hql = "select u from Users u where u.username like :p1 and u.password like :p2";
          session = PersistenceManager.getInstance().getSessionFactory().openSession();
    
          Query q = session.createQuery(hql)
            .setParameter("p1", username)
            .setParameter("p2", password);
    
          if (q.list().size() == 0)
          {
            session.close();
            return new Users();
          }
    
          Users user = (Users)q.list().get(0);
          session.close();
          return user;
        }
        catch (Exception e)
        {
          session.close();
         
        }
      }
    

    Example 2:

    import org.hibernate.HibernateException;
    import org.hibernate.Query;
    import org.hibernate.Session;
     public String Registration(Users u) { 
    
        Session session = null;
        try
        {
          String hql = "select u from Users u where u.username like :p1";
    
          session = PersistenceManager.getInstance().getSessionFactory().openSession();
    
          Query q = session.createQuery(hql).setParameter("p1", u.getUsername());
    
          if (q.list().size() == 0)
          {
            session.beginTransaction();
            session.persist(u);
            session.getTransaction().commit();
            session.close();
            return new Boolean(true).toString();
          }
    
          session.close();
    
          return new Boolean(false).toString();
        }
        catch (Exception e)
        {
          return e.toString();
        }
      }
    
    0 讨论(0)
  • 2020-12-18 04:16

    I had the same issue just because I forgot to wrap my hibernate code in a transaction. Apparently the connection to DB is not closed until you commit a transaction. Here is a valid example:

    Session session = HibernateUtils.getSession();
    Transaction tx = session.beginTransaction();
    Category cat = null;
    
    try{
        cat = (Category)session.get(Category.class, 1);
        tx.commit();
    }catch(Exception ex){
        tx.rollback();
    }finally{           
        session.close();
    }
    
    return cat;
    
    0 讨论(0)
提交回复
热议问题