Java 冬眠:can';t将项目保存到sqlite数据库中。总是得到;“数据库已锁定”;例外
我试图将hibernate5与sqlite3一起使用,但总是遇到异常Java 冬眠:can';t将项目保存到sqlite数据库中。总是得到;“数据库已锁定”;例外,java,hibernate,sqlite,Java,Hibernate,Sqlite,我试图将hibernate5与sqlite3一起使用,但总是遇到异常 org.hibernate.TransactionException: Unable to commit against JDBC Connection at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.commit(AbstractLogicalConnectionImplementor.java:86)
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方言下载自:
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>
我真的很困惑,因为我的代码很简单,但不起作用。我需要做什么来避免“数据库被锁定”异常
编辑:我不同意这个问题是重复的。在这个问题的答案中,我没有找到一个适合我的解决方案。而且我的db文件只有一个工作进程(fuser dbfile)。这看起来像是Hibernate的一个bug或特性。将
添加到JPA配置中可能会解决它(至少在我的情况下是这样)。将添加到JPA配置中可能会解决它(至少在我的情况下是这样)。在刷新之前你试过提交吗?Jay,我试过了,但它不起作用。首先,这不是上述问题的重复。你找到解决办法了吗?我发现这是因为关联(例如一对多),但我找不到任何解决方案。对于最终来到这里的人,将
添加到JPA配置解决了我的问题。@Tunaki,你同意上面的评估(在评论和问题编辑中)吗,这不是所指出的dup?你在刷新之前尝试过提交吗?Jay,我试过了,但没有成功。首先,这不是上述问题的重复。你找到解决办法了吗?我发现这是因为关联(例如,一对多),但我找不到任何解决方案。对于最终来到这里的人,将
添加到JPA配置解决了我的问题。@Tunaki,您是否同意上面的评估(在评论和问题编辑中),即这不是所示的dup?
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;
}