我正在尝试将hibernate5与sqlite3一起使用,但总是会出现异常
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)
操作系统:Ubuntu 14.04
从https://github.com/gwenn/sqlite-dialect
下载的SQLite方言我的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>
我的代码(非常简单):
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;
}
我真的很困惑,因为我的代码很简单,但它不起作用。我需要做些什么来避免&#34;数据库被锁定&#34;异常?
编辑:我不同意这个问题与How do I unlock a SQLite database?重复。在那个问题的答案中,我找不到任何一个有效的解决方案。此外,我的db文件(fuser dbfile)只有一个工作进程。它看起来像是Hibernate的一个bug或特性。
答案 0 :(得分:0)
将<property name="hibernate.connection.autocommit" value="true"/>
添加到JPA配置可以解决它(至少在我的情况下是这样做的。)