gpt4 book ai didi

java - Hibernate:无法将项目保存到 sqlite 数据库中。总是出现 "Database is locked"异常

转载 作者:太空宇宙 更新时间:2023-11-04 12:55:38 27 4
gpt4 key购买 nike

我尝试将 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

SQLite 方言下载自:https://github.com/gwenn/sqlite-dialect

我的 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;
}

我真的很困惑,因为我的代码很简单,但它不起作用。我需要做什么才能避免“数据库已锁定”异常?

编辑:我不同意这个问题与How do I unlock a SQLite database?重复。 。我在该问题的答案中没有找到适合我的解决方案。另外,我的数据库文件(fuser dbfile)只有一个工作进程。它看起来像是 Hibernate 的一个错误或特性。

最佳答案

添加<property name="hibernate.connection.autocommit" value="true"/> JPA 配置可能会解决它(至少在我的情况下它做到了)。

关于java - Hibernate:无法将项目保存到 sqlite 数据库中。总是出现 "Database is locked"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35418684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com