Spring中的数据库连接管理 [英] Database connection management in Spring
问题描述
我们必须在使用Spring Framework时明确管理数据库资源。喜欢关闭所有打开的连接等吗?
Do we have to explicitly manage database resources when using Spring Framework.. liking closing all open connections etc?
我读过Spring从这样的锅炉板编码...
I have read that Spring relieves developer from such boiler plate coding...
这是为了回答我在Spring web应用程序中遇到的错误:
This is to answer an error that I am getting in a Spring web app:
org.springframework.jdbc.CannotGetJdbcConnectionException:
无法获取JDBC连接;嵌套
异常是java.sql.SQLException:
ORA-00020:最大进程数量
(150)超过
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: ORA-00020: maximum number of processes (150) exceeded
jdbcTemplate
是在xml文件中配置的,DAO实现引用了 jdbcTemplate
bean
The jdbcTemplate
is configured in the xml file and the DAO implementation has reference to this jdbcTemplate
bean which is used to query the database.
推荐答案
我们必须在使用时明确管理数据库资源Spring框架,像关闭所有打开的连接等?
Do we have to explicitly manage database resources when using Spring Framework, like closing all open connections etc?
如果你正在使用Spring的抽象类似JbdcTemplate,Spring会为你处理这个错误,部分。
If you are using Spring abstraction like JbdcTemplate, Spring handles that for you and it is extremely unlikely that that there is a bug in that part.
现在,没有关于您的配置(您的applicationContext.xml),上下文(如何创建您的应用程序上下文,什么时候完全发生?)的更多信息,这是很难说的什么。所以这是一个黑暗的镜头:你有属性 destroy-method =关闭
设置你的数据源配置?类似的东西:
Now, without more information on your configuration (your applicationContext.xml), on the context (how do you create your application context, when does this happen exactly?), it is a hard to say anything. So this is a shot in the dark: do you have the attribute destroy-method="close"
set on your datasource configuration? Something like that:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
在某些情况下,不使用destroy-method结合一些其他不良做法,耗尽资源。
In certain circumstances, not using the destroy-method combined with some other bad practices may eventually end up with exhausting resources.
这篇关于Spring中的数据库连接管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!