首页 文章

Hibernate连接超时

提问于
浏览
2

我们的服务器中的mysql连接有问题 . WebApp工作正常,但几个小时后,我们从与mysql的连接中得到一个错误 . 我们认为hibernate Session出了问题 . 据我们所知,当hibernate尝试使用相同的会话时,mysql连接被关闭 . 也许我们没有正确地结束 Session .

我们使用Struts 2,我们已经定义了一个管理所有会话内容的Interceptor . 这是代码:

public class OSVStrutsInterceptors implements Interceptor {

private  static SessionFactory sf;

@Override
public void destroy() {

    try
    {
        sf.getCurrentSession().close();
    }
    catch (HibernateException ex)
    {
        throw new RuntimeException(ex);
    }
}

@Override
public void init() {

}

@Override
public String intercept(ActionInvocation ai) throws Exception {
    sf = HibernateUtil.getSessionFactory();
    Session sesion=sf.getCurrentSession();
    try {
    sesion.beginTransaction();
    }
    catch (Exception ex ) {
        System.out.println(new Date()+" Sesion cerrada. Obtenemos una nueva");
        sesion=sf.openSession();
    }

    String result=ai.invoke();
    sesion.getTransaction().commit();

    return result;

}
}

如您所见,我们正在尝试获取currentSession,但如果不可能,我们将打开一个新的Session . 我们还关闭了拦截器的destroy()方法的会话 .

但我们总是在我们的服务器上收到此错误:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: Broken pipe

STACKTRACE:

java.net.SocketException: Broken pipe

3 回答

  • 4

    通信链路故障可能由于多种原因而发生,包括网络中断,路由器配置错误或其他网络问题 . 也可能(尽管不太可能)你的连接只是超时 . 在任何情况下,您都可以尝试通过将MySQL数据源配置为自动重新连接来解决问题 . 一种方法是在JDBC URL上指定适当的属性:

    url="jdbc:mysql://localhost:3306/mydb?autoReconnect=true
    
  • 0

    我使用的是使用org.apache.commons.dbcp.BasicDataSource的自定义ConnectionProvider . 在这个课程中你必须设置:

    setValidationQuery("select 1");
          setTestOnBorrow(true);
    

    注意:我用net.sf.log4jdbc.ConnectionSpy包装连接,但你不能这样做

    在hibernate.cfg中

    <property name="hibernate.connection.provider_class">
        example.DBCPConnectionProvider
    </property>
    
    
    
    
    
    public class DBCPConnectionProvider implements ConnectionProvider { 
        private static final long serialVersionUID = -4063997930726759172L;
    
        private static final Logger log = Logger.getLogger(DBCPConnectionProvider.class);
    
        static CustomDataSource ds;
    
        static{
            try {
                ds = new CustomDataSource();    
            } catch (Exception e) {
                log.error("", e);
            }
        }
    
    
        @Override
        public void closeConnection(Connection conn) throws SQLException {
            conn.close();
        }
    
        @Override
        public Connection getConnection() throws SQLException {
            return ds.getConnection();
        }
    
    
    
    
        @Override
        public boolean supportsAggressiveRelease() { 
            return true;
        }
    
        @Override
        public boolean isUnwrappableAs(Class arg0) {
            return false;
        }
    
        @Override
        public <T> T unwrap(Class<T> arg0) {
            return null;
        }
    
        }
    
    
    
    
    
    
    
    
    
    public class CustomDataSource extends BasicDataSource{
        private static final Logger log = Logger.getLogger(CustomDataSource.class);
    
    
        public CustomDataSource(){  
            Properties props = HibernateConfiguration.getProperties(); 
              setDriverClassName( props.getProperty("hibernate.connection.driver_class")  );
              setUsername(  props.getProperty("hibernate.connection.username") );
              setPassword( props.getProperty("hibernate.connection.password")  );
              setUrl( props.getProperty("hibernate.connection.url")  ); 
              setValidationQuery("select 1");
              setTestOnBorrow(true);
    
        }
    
        @Override
        public Connection getConnection() throws SQLException{ 
    
            return   new net.sf.log4jdbc.ConnectionSpy( super.getConnection() );
        }
    }
    
  • 1

    MySQL不推荐添加 autoReconnect=true (找不到引用) . 而是在 hibernate.cfg.xml 上添加这些行

    <!--  c3p0 -->
    <property name="hibernate.c3p0.validate">true</property>
    <property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">600</property>
    <property name="hibernate.c3p0.timeout">1800</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>
    <property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
    

    并将这些库添加到 CLASSPATH

    hibernate-c3p0-4.0.0.Final.jar
    c3p0-0.9.1.jar (Not sure if this is neccessary)
    

相关问题