2011-09-26 5 views
2

Tomcat으로 내 앱을 실행하려고하는데, 처음으로 앱을 디버깅/실행하는 것이 처음입니다. 하지만 두 번째로 실행하려고 할 때 "XJ040"오류가 발생합니다. 내 서버를 중지 할 때 connections.Because을 닫는에 문제가 문제가 떨어져 두 번째 실행까지 이동하기 때문에서블릿을 사용하여 db 연결을 닫는 중 문제가 발생했습니다.

 "Failed to start database 'C:\Documents and Settings\vitaly87\.netbeans-  derby\articals' with class loader WebappClassLoader 
     context: /WebApplication1 
    delegate: false 
     repositories: 

나는 문제를 생각한다. 여기

코드 : 당신은 예외가있을 경우 코드에서 마지막으로

connect = DriverManager.getConnection(...) 

try 
{ 
    // use connection 
} 
finally 
{ 
    try 
    { 
     connect.close() 
    } 
    catch (SQLException e) 
    { 
     // log e 
    } 
} 

에 연결을

+0

닫는 기능의 시작 부분에 print 문을 두어 왜 실행되는지 확인하지 않는 이유는 무엇입니까? – BobTurbo

답변

1

최저 항상 가까운 연결을 돕는

 private Connection connect = null; 
//private Statement stmt = null; 
private PreparedStatement preparedStatement = null; 
private ResultSet resultSet = null; 
    public ArrayList<story> stories=new ArrayList<story>(); 
      void getStories(String version) throws SQLException{ 

     try{ 

     Class.forName("org.apache.derby.jdbc.EmbeddedDriver"); 

      }catch(ClassNotFoundException e){ 
       System.out.println(e); 
      } 
      connect = DriverManager.getConnection("jdbc:derby:C:\\Documents and Settings\\vitaly87\\.netbeans-derby\\articals", "admin", "admin"); 
     // statement = connect.createStatement(); 
       int ArticlesId= Integer.parseInt(version); 
      preparedStatement = connect.prepareStatement("SELECT * FROM admin.articles where id>"+ArticlesId+""); 
       resultSet = preparedStatement.executeQuery(); 
      while (resultSet.next()) { 
    stories.add(new  story(resultSet.getString("title"),resultSet.getString("date"),resultSet.getString("text"))); 
} 
      close(); 
      } 
      //close connection 
private void close() { 
    try { 
     if (resultSet != null) { 
      resultSet.close(); 
     } 



     if (connect != null) { 
      connect.close(); 
     } 
    } catch (Exception e) { 

    } 

감사 닫히지 않습니다 예 : parseInt (버전) 또는 exequteQuery()

요 당신이 어쨌든 연결을 닫고 있기 때문에 결과 집합의 닫기가 필요하다고 생각하지 않습니다. 이 방법에 던져 예외를 볼 수 없습니다 resultSet.close()는 연결이 종료되지 않습니다 예외가 발생하는 경우 1.과 2. 때문에

try { 
    if (resultSet != null) { 
     resultSet.close(); 
    } 

    if (connect != null) { 
     connect.close(); 
    } 
} catch (Exception e) { 

} 

는 문제가있다. 적어도 catch 된 예외를 기록하는 것이 좋습니다.

+0

한 가지 추가 사항 : 메서드에서 사용 후 문 및 연결을 닫은 경우 클래스 멤버로 메서드간에 공유하지 말고 대신 로컬 변수를 사용하는 것이 좋습니다. 그렇게하지 않으면 무언가를 재사용 할 위험이 줄어 듭니다. – Gandalf

관련 문제