2016-09-18 3 views
1

는 내가 folowing dbConnection에게java를 사용하여 sqlite에 데이터베이스 연결이 있는지 확인하는 방법은 무엇입니까?

public class DBConnection { 

    protected Statement statement; 
    protected Connection connection = null; 

    public DBConnection() { 
     try { 
      Class.forName("org.sqlite.JDBC"); 
     } catch (ClassNotFoundException e1) { 
      e1.printStackTrace(); 
     } 
     try { 

      connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\ion\\Desktop\\sample.db"); 
       statement = connection.createStatement(); 
       statement.setQueryTimeout(30); 

       statement.executeUpdate("CREATE TABLE IF NOT EXISTS person (id STRING PRIMARY KEY NOT NULL," 
         + "name STRING, email STRING UNIQUE, password STRING)"); 

       statement.executeUpdate("CREATE TABLE IF NOT EXISTS message (id STRING PRIMARY KEY NOT NULL," 
         + "text STRING, subject STRING, dateMessage Date, parrentMessageId String , personId String, " 
         + "categoryId String," 
         + " FOREIGN KEY(personId) REFERENCES person(id),FOREIGN KEY(categoryId) REFERENCES category(id))"); 

       statement.executeUpdate(
         "CREATE TABLE IF NOT EXISTS category (id STRING PRIMARY KEY NOT NULL," + "name STRING)"); 



     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

을 가지고 있지만 나는이

String id = personService.getIdByEmail(email); 

     message.setPersonId(id); 
     messageService.persist(message); 

같은 서블릿에서 사용하고 난 다른 저장소 호출 두 번 다른 서비스를 호출하는 경우 (personRep 및 messageRep을이 DbConnection 클래스를 확장 모두) 그것은 나를주고있다 "database is locked"

연결이 이미 있는지 어떻게 확인합니까? 도와주세요 .. 고마워요 !!

답변

0

두 개의 장치를 사용 중이며 동시에 두 개의 서비스를 호출합니다. 따라서 서버는 두 개의 스레드를 생성하고 두 개의 요청을 처리합니다. 이 두 개의 머리는 동시에 두 개의 연결을 열 것입니다. 이 연결을 사용하면 스레드가 CREATE TABLE 명령이있는 SQL 문을 실행합니다. 이것은 DDL 표현식입니다. 그래서 하나의 스레드는 데이터베이스 잠금 및 다른 스레드 병목 현상을 가져오고 '데이터베이스 잠금'과 같은 예외를 제공합니다.

동기화를 사용하면 하나의 스레드 만 코드를 실행할 수 있습니다. 다른 사람은 끝날 때까지 기다려야합니다.

자세한 내용 https://www.sqlite.org/cvstrac/wiki?p=DatabaseIsLocked

+0

이와 비슷한 기능이 있습니까? 동기 (lock1) { \t \t \t \t \t \t \t \t 연결 = DriverManager.getConnection를 ("JDBC : SQLite는 : C : \\ 사용자 \\ 이온 \\ 화상 \\ sample.db"); \t \t \t \t statement = connection.createStatement(); \t \t \t \t statement.setQueryTimeout (30); – joesid

관련 문제