spring + SQLite在多线程应用程序中 [英] spring + SQLite in multi-threaded application
问题描述
我正在开发一个使用SQLite数据库和spring的应用程序。多线程尝试修改数据库时遇到问题 - 我收到错误:
I'm developing an application that uses SQLite database and spring. I have problems when multiple threads try to modify the database - I get an error:
'数据库文件被锁定'
我配置了一个数据源:
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" lazy-init="true">
<property name="driverClassName" value="org.sqlite.JDBC" />
<property name="url" value="jdbc:sqlite:sample.db" />
<property name="initialSize" value="2" />
<property name="maxActive" value="20" />
<property name="maxIdle" value="5" />
<property name="poolPreparedStatements" value="true" />
</bean>
在每个线程中,我有一个单独的JdbcDaoSupport实例,它执行对数据库的插入:
and in each thread I have a separate instance of the JdbcDaoSupport that performs an insert to the database:
getJdbcTemplate().update(
"insert into counts values(15)"
);
执行数据库更新的函数是事务性的(我已尝试过所有隔离级别,在每种情况下我犯了同样的错误)。
The function that performs the database update is transactional (I've tried all isolation levels, in each case I get the same error).
使用其他数据库(MySql)时,相同的代码工作正常。
The same code works fine, when using other database (MySql).
如何解决这个问题(在我的代码中没有添加'手动'同步)?
How can I solve this (without adding a 'manual' synchronization in my code)?
推荐答案
我没有尝试过,但我建议,鉴于SQLite一次只支持一个连接,您应该将数据源配置为只创建一个连接。
I've not tried it, but I'd suggest that, given that SQLite supports only one connection at a time, you should configure your data source to only ever create one connection.
我认为这将是以下内容......
I think that would be something like the following...
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="true">
<property name="driverClassName" value="org.sqlite.JDBC" />
<property name="url" value="jdbc:sqlite:sample.db" /> <
<property name="initialSize" value="1" />
<property name="maxActive" value="1" />
<property name="maxIdle" value="1" />
<property name="poolPreparedStatements" value="true" />
</bean>
这篇关于spring + SQLite在多线程应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!