1

I am trying to use hibernate5 with sqlite3, but always getting exception

org.hibernate.TransactionException: Unable to commit against JDBC Connection
    at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.commit(AbstractLogicalConnectionImplementor.java:86)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
    at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
    at market.dal.hibernate.HibernatePurchaseHistoryRepository.saveAll(HibernatePurchaseHistoryRepository.java:20)
    at jobs.LoadLastPurchasesJob.execute(LoadLastPurchasesJob.java:52)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.sql.SQLException: database is locked
    at org.sqlite.core.DB.throwex(DB.java:859)
    at org.sqlite.core.DB.exec(DB.java:142)
    at org.sqlite.jdbc3.JDBC3Connection.commit(JDBC3Connection.java:165)

OS: Ubuntu 14.04

SQLite dialect downloaded from: https://github.com/gwenn/sqlite-dialect

My hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="show_sql">false</property>
        <property name="format_sql">true</property>
        <property name="dialect">org.hibernate.SQLiteDialect</property>
        <property name="connection.driver_class">org.sqlite.JDBC</property>
        <property name="connection.url">jdbc:sqlite:mydb.db</property>
        <property name="connection.username">admin</property>
        <property name="connection.password">admin</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>


        <mapping resource="market/dal/hibernate/mapping/TestItem.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

My code (very simple):

 public static void main(String[] args) throws Exception {
        SessionFactory sessionFactory = configureHibernateSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        try {
            try {
                session.saveOrUpdate(new TestItem(1,"test"));
                session.flush();
                transaction.commit();
            } catch (Exception e) {
                transaction.rollback();
                e.printStackTrace();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
 }

 private static SessionFactory configureHibernateSessionFactory() throws HibernateException {
        // A SessionFactory is set up once for an application
        sessionFactory = new Configuration()
                .configure() // configures settings from hibernate.cfg.xml
                .buildSessionFactory();
        return sessionFactory;
  }

I was really confused, because my code is very simple, but it doesn't work. What i need to do to avoid "database is locked" exception?

EDIT: I disagree that this question is duplicate of How do I unlock a SQLite database?. I not found no one working solution for me in that question answers. Also i have only one working process for my db file (fuser dbfile). It looks like a bug or peculiarity of Hibernate.

halfer
  • 19,824
  • 17
  • 99
  • 186
Frank59
  • 3,141
  • 4
  • 31
  • 53
  • Have you tried commiting before you flush? – Jay Feb 15 '16 at 20:49
  • Jay, i tried and it doesn't work. – Frank59 Feb 16 '16 at 17:40
  • Firstly, it was not a duplication of the mentioned question. And did you find any solution? I figured it out it is because of associations (e.g. one-to-many) but i couldn't find any solution. – Erfan Jul 03 '17 at 12:11
  • 1
    For people who end up here, adding `` to JPA config solved my problem. – Erfan Jul 03 '17 at 13:05
  • @Tunaki, do you agree with the assessment above (in comments and in a question edit) that this is not a dup as indicated? – halfer Aug 28 '17 at 18:02
  • This looks like a SQLite+Hibernate problem, whereas the suggested duplicate is just SQLite. In absence of a reply to my request above, I am voting to reopen. – halfer Sep 17 '17 at 09:32
  • @Erfan, if this re-opens, would you add your answer from the comments above? – halfer Sep 17 '17 at 09:34
  • @halfer, sure, it may be useful for others as well. – Erfan Sep 17 '17 at 10:50

1 Answers1

0

Adding <property name="hibernate.connection.autocommit" value="true"/> to JPA config may solve it (at least it did it in my case).

Erfan
  • 1,132
  • 15
  • 21