Java Spring中的数据库连接管理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2249993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Database connection management in Spring
提问by ria
Do we have to explicitly manage database resources when using Spring Framework.. liking closing all open connections etc?
使用 Spring Framework 时,我们是否必须显式管理数据库资源.. 喜欢关闭所有打开的连接等?
I have read that Spring relieves developer from such boiler plate coding...
我读过 Spring 使开发人员摆脱了这种样板编码......
This is to answer an error that I am getting in a Spring web app:
这是为了回答我在 Spring Web 应用程序中遇到的错误:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: ORA-00020: maximum number of processes (150) exceeded
org.springframework.jdbc.CannotGetJdbcConnectionException: 无法获得 JDBC 连接;嵌套异常是 java.sql.SQLException: ORA-00020: 超过最大进程数 (150)
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.
将jdbcTemplate
在XML文件中配置和DAO实现具有参考这个jdbcTemplate
豆是用来查询数据库。
回答by Nathan Voxland
It could be due to connections not being closed. How are you accessing your connections within spring? Are you are using JdbcTemplate to query the database? Or just getting the connection from spring?
可能是因为连接没有关闭。您如何在春季访问您的连接?你是在使用 JdbcTemplate 来查询数据库吗?或者只是从春天获得连接?
回答by Pascal Thivent
Do we have to explicitly manage database resources when using Spring Framework, like closing all open connections etc?
使用 Spring Framework 时是否必须显式管理数据库资源,例如关闭所有打开的连接等?
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.
如果您使用 JbdcTemplate 等 Spring 抽象,Spring 会为您处理,并且该部分极不可能存在错误。
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:
现在,如果没有关于您的配置(您的 applicationContext.xml)的更多信息,关于上下文(您如何创建您的应用程序上下文,这究竟何时发生?),很难说什么。所以这是在黑暗中的一个镜头:您是否在destroy-method="close"
数据源配置中设置了属性?类似的东西:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
In certain circumstances, not using the destroy-method combined with some other bad practices may eventually end up with exhausting resources.
在某些情况下,不使用 destroy-method 结合其他一些不良做法可能最终会耗尽资源。
回答by skaffman
I have read that Spring relieves developer from such boiler plate coding
我读过 Spring 将开发人员从这种样板编码中解放出来
That depends which level of Spring you operate at. JdbcTemplate
provides many different operations, some of which are fire-and-forget, some of which still require you to manage your JDBC resources (connections, resultsets, statements, etc) properly. The rule of thumb is that if you find yourself calling getConnection()
, then at some point you need to call releaseConnection()
also.
这取决于您操作的 Spring 级别。JdbcTemplate
提供了许多不同的操作,其中一些是一劳永逸的,其中一些仍然需要您正确管理 JDBC 资源(连接、结果集、语句等)。经验法则是,如果您发现自己在调用getConnection()
,那么在某些时候您也需要调用releaseConnection()
。
ORA-00020: maximum number of processes (150) exceeded
ORA-00020: 超过最大进程数 (150)
Are you using a connection pool? If so, then make sure that it isn't configured with a larger number of max connections than your database is capable of handling (150, in this case). If you're not using a connection pool, then you really, really should be.
你在使用连接池吗?如果是这样,请确保它配置的最大连接数没有超过您的数据库能够处理的数量(在本例中为 150)。如果您没有使用连接池,那么您真的,真的应该使用。
回答by Nathan Voxland
you say "The jdbcTemplate is configured in the xml file". You should normally create a new instance of the jdbcTemplate for each usage, not have it managed by spring.
您说“在 xml 文件中配置了 jdbcTemplate”。您通常应该为每次使用创建一个 jdbcTemplate 的新实例,而不是由 spring 管理它。
I would guess that each time you request a new jdbcTemplate bean from spring, it is creating a new one with a new connection to the database, but after it falls out of scope in your code it is still referenced by spring's applicationContext, and so does not close the connection.
我猜每次你从 spring 请求一个新的 jdbcTemplate bean 时,它都会创建一个新的,并与数据库建立一个新的连接,但是在它超出你的代码范围后,它仍然被 spring 的 applicationContext 引用,也是如此不关闭连接。
回答by Sudhakar Krishnan
My hosing provide only 20 connection. I done by manually close the connection on every request to db. I not declared a destory-method in bean(this not worked "i dont know why"), but i done in every requst call. (Hint : extends JdbcDaoSupport in dao class).
我的软管仅提供 20 个连接。我通过手动关闭对数据库的每个请求的连接来完成。我没有在 bean 中声明销毁方法(这不起作用“我不知道为什么”),但我在每个请求调用中都完成了。(提示:在 dao 类中扩展 JdbcDaoSupport)。
public void cleanUp() {
try {
if (!this.getJdbcTemplate().getDataSource().getConnection().isClosed()) {
this.getJdbcTemplate().getDataSource().getConnection().close();
}
} catch (Exception e) {
Logger.getLogger(myDAOImpl.class.getName()).log(Level.SEVERE, null, e);
}
}