2012-03-14 2 views
2

우리 서버에 mysql 연결에 문제가 있습니다. 웹 애플리케이션은 올바로 작동하지만 몇 시간이 지나면 mysql과의 연결에서 오류가 발생합니다. 최대 절전 모드 세션에 문제가 있다고 생각합니다. 우리가 아는 한, 최대 절전 모드가 동일한 세션을 사용하려고 시도하는 동안 mysql 연결은 닫힙니다. 아마도 세션을 제대로 닫지 않을 것입니다.최대 절전 모드 연결 시간 초과

우리는 Struts 2와 함께 작업하며 모든 세션 작업을 관리하는 인터셉터를 정의했습니다. 다음은 코드입니다 : 당신이 볼 수 있듯이, 우리는 currentSession를 얻기 위해 노력하고있다

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; 

} 
} 

하지만 가능하지 않은 경우, 우리는 새로운 세션을 엽니 다. 또한 인터셉터의 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 

답변

4

autoReconnect=true은 MySQL이 사용하지 않는 것이 좋습니다 추가 (참조를 찾을 수 없습니다). 대신 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> 

에 다음 줄을 추가 내가 org.apache.commons.dbcp.BasicDataSource를 사용하는 사용자 정의 ConnectionProvider를를 사용하여 CLASSPATH

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

통신 링크 장애는 네트워크 중단, 라우터 잘못 또는 다른 네트워크 문제를 포함 이유로 수에 대한이 일어날 수 있습니다. 당신의 연결이 단순히 시간 초과되는 것도 가능합니다. 어쨌든 MySQL 데이터 소스가 자동으로 다시 연결되도록 구성하여 문제를 해결할 수 있습니다. 이 일을하는 한 가지 방법은 JDBC의 URL에 해당하는 속성을 지정하는 것입니다 :

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

에이 라이브러리를 추가합니다.

 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()); 
    } 
} 
관련 문제