2014-03-30 27 views
0

我正在開發一個應用程序3部分: - JavaFX桌面應用程序。 - Java Server WebApp - AndroidApp併發訪問SQLite Da

我正在使用Hibernate來映射SQLite數據庫。

但是,當桌面應用程序是開放的,並嘗試從AndroidApp插入新ibject通過量服務器時,它給我一個錯誤:java.sql.SQLException: database is locked

我的hibernate.cfg.xml文件:

<property name="show_sql">true</property> 
<property name="format_sql">true</property> 
    <property name="dialect">dialect.SQLiteDialect</property> 
    <property name="connection.driver_class">org.sqlite.JDBC</property> 
    <property name="connection.url">jdbc:sqlite:grainsa_provisional.sqlite</property> 
    <property name="connection.username"></property> 
    <property name="connection.password"></property> 

而我在服務器和桌面「對象管理器」,以同樣的方式舉例:

private Session mSession; 
private Transaction mTransaction; 

private void initQuery() throws HibernateException { 
    mSession = HibernateUtil.getSessionFactory().openSession(); 
    mTransaction = mSession.beginTransaction(); 
} 

private void manejaExcepcion(HibernateException hibernateException) { 
    mTransaction.rollback(); 
    throw new HibernateException("ha ocurrido un error con la Base de Datos!!!", hibernateException); 
} 


public Conductor selectConductorByID(Integer id) { 
    Conductor conductor = new Conductor(); 
    try{ 
     initQuery(); 
     conductor = (Conductor) mSession.get(Conductor.class, id); 
    } catch (HibernateException e){ 
     manejaExcepcion(e); 
     throw e; 
    } finally { 
     mSession.close(); 
    } 
    return conductor; 
} 

如果您需要了解更多信息請諮詢!

我做錯了什麼?

謝謝大家,對不起我的英語!

編輯:ím正在考慮更改mi桌面JavaFX應用程序的訪問模式,以便通過服務器進行查詢,但這需要我很多時間,而且我認爲這不是最好的方式。

編輯2: 這是正確的方式來打開,查詢和關閉數據庫的conexion鎖定/查詢/解鎖?

private void initQuery() throws HibernateException { 
    mSession = HibernateUtil.getSessionFactory().openSession(); 
    mTransaction = mSession.beginTransaction(); 
} 

private void manejaExcepcion(HibernateException hibernateException) { 
    mTransaction.rollback(); 
    throw new HibernateException("ha ocurrido un error con la Base de Datos!!!", hibernateException); 
} 


public Conductor selectConductorByID(Integer id) { 
    Conductor conductor = new Conductor(); 
    try{ 
     initQuery(); 
     conductor = (Conductor) mSession.get(Conductor.class, id); 
    } catch (HibernateException e){ 
     manejaExcepcion(e); 
     throw e; 
    } finally { 
     mSession.close(); 
    } 
    return conductor; 
} 

請幫忙!並再次感謝! I'm有點deseperated ...

+0

我不知道如果這有助於以任何方式,因爲我沒有看到你的查詢,以及如何你打電話給他們,但我看到[這篇文章](http://stackoverflow.com/a/5659027/1300817),它說那裏「調用sqlite3_finalize(compiledStatement);'和'sqlite3_close(數據庫);'最終確定編譯語句並關閉數據庫 –

回答

0

從常見問題解答(5)在SQLite的常見問題解答:

But use caution: this locking mechanism might not work correctly if the database file is kept on an NFS filesystem. This is because fcntl() file locking is broken on many NFS implementations. You should avoid putting SQLite database files on NFS if multiple processes might try to access the file at the same time. On Windows, Microsoft's documentation says that locking may not work under FAT filesystems if you are not running the Share.exe daemon. People who have a lot of experience with Windows tell me that file locking of network files is very buggy and is not dependable. If what they say is true, sharing an SQLite database between two or more Windows machines might cause unexpected problems.

也許這是你的問題的原因是什麼?你在做窗戶嗎?

Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.

SQLLite在多用戶場景中存在問題,但如果更新短而快,仍然可以正常工作。
在你的代碼中,它看起來像你沒有正確地關閉事務。 也許這種情況下,數據庫鎖定

你應該改變代碼,看看休眠DOC here

private Transaction initQuery() throws HibernateException { 
    mSession = HibernateUtil.getSessionFactory().openSession(); 
    mTransaction = mSession.beginTransaction(); 
    return mTransaction;  
} 


public Conductor selectConductorByID(Integer id) { 
    Conductor conductor = new Conductor(); 
    Transaction tx = null; 
    try{ 
     tx = initQuery(); 
     conductor = (Conductor) mSession.get(Conductor.class, id); 
     //flush and commit before close 
     mSession.flush(); 
     tx.commit(); 
    } catch (HibernateException e){ 
     manejaExcepcion(e); 
     throw e; 
    } finally { 
     mSession.close(); 
    } 
    return conductor; 
} 
+0

非常感謝,我會嘗試一下,我告訴你! – vlopezla

+0

我旅行,我希望這個週末我會嘗試解決方案 – vlopezla

+0

沒問題,謝謝你的更新:-) –