Hibernate sqlite SQLITE_BUSY

时间:2015-03-31 09:12:09

标签: java hibernate sqlite

我试图在多线程应用程序中使用hibernate和sqlite并获取SQLITE_BUSY错误:

[SQLITE_BUSY]  The database file is locked (database is locked)

在1个线程中重现错误的简化代码:

Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(new Object());
session.flush();

Session session2 = sessionFactory.openSession();
session2.beginTransaction();
session2.save(new Object());
session2.getTransaction().commit(); 

广泛建议我将pool_size设置为1,但它没有帮助

<property name="connection.pool_size">1</property>
<property name="hibernate.connection.pool_size">1</property>
我使用的

包:

sqlite-jdbc: 3.8.7
hibernate: 4.3.8

1 个答案:

答案 0 :(得分:1)

自定义连接池hibernate-c3p0完成了这项工作。 只需添加到pom.xml:

<dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-c3p0</artifactId>
      <version>4.3.7.Final</version>
</dependency>

到hibernate.cfg.xml:

<property name="hibernate.c3p0.min_size">1</property>
<property name="hibernate.c3p0.max_size">1</property>