Redian新闻
>
Java GC 为什么不能自动释放jdbc connection?
avatar
Java GC 为什么不能自动释放jdbc connection?# Java - 爪哇娇娃
T*x
1
比如在一个函数内部,connection是一个local variable。
函数结束的时候为什么不自动释放jdbc connection?
做不到吗?内部有什么难度吗?
avatar
w*z
2
it is more than memory, it is resource. Java 7 comes with closable to
rescure.

【在 T*******x 的大作中提到】
: 比如在一个函数内部,connection是一个local variable。
: 函数结束的时候为什么不自动释放jdbc connection?
: 做不到吗?内部有什么难度吗?

avatar
f*t
3
GC是一个独立的thread,变量生命周期结束后不确定什么时候会被释放。
avatar
a*w
4
说句题外话,即使只涉及内存,有些内存GC也不会动,比如
obsolete object reference

【在 w**z 的大作中提到】
: it is more than memory, it is resource. Java 7 comes with closable to
: rescure.

avatar
c*e
5
conn.close();
不就行了。

【在 T*******x 的大作中提到】
: 比如在一个函数内部,connection是一个local variable。
: 函数结束的时候为什么不自动释放jdbc connection?
: 做不到吗?内部有什么难度吗?

avatar
w*z
6
不过,Java的resource close 是比较恶心,conn.close 也要放在try catch 里, 都
是没用的。
try-with-resource is a bit better

【在 c*********e 的大作中提到】
: conn.close();
: 不就行了。

avatar
w*z
7
不过,Java的resource close 是比较恶心,conn.close 也要放在try catch 里, 都
是没用的。
try-with-resource is a bit better

【在 c*********e 的大作中提到】
: conn.close();
: 不就行了。

avatar
c*e
8
有的把conn.close()还放在finally里。这玩艺都是约定俗成了,抄人家的code,也都是
这么写的。
c#里用using( ).

【在 w**z 的大作中提到】
: 不过,Java的resource close 是比较恶心,conn.close 也要放在try catch 里, 都
: 是没用的。
: try-with-resource is a bit better

avatar
T*x
9
据说在finally里写是最好的:
try {
----Connection connection = dataSource.getConnection();
----Statement statement = connection.prepareStatement(sql);
----ResultSet result = statement.executeQuery();
...
} catch (Exception e) {
...
} finally {
----if (result != null) {
--------try {
------------result.close();
--------} catch (Exception e) {
--------}
----}
----if (statement != null) {
--------try {
------------statement.close();
--------} catch (Exception e) {
--------}
----}
----if (connection != null) {
--------try {
------------connection.close();
--------} catch (Exception e) {
--------}
----}
}
这么一大堆,好像是标准吧?
感觉挺麻烦的。而且一个函数中只能一个try跟一个finally。
多个try和多个finally,我感觉出口很复杂,可能会出漏洞。

【在 c*********e 的大作中提到】
: conn.close();
: 不就行了。

avatar
g*g
10
I think close connection is good enough. Or you can use JdbcTemplate and you
don't need to do anything at all.

【在 T*******x 的大作中提到】
: 据说在finally里写是最好的:
: try {
: ----Connection connection = dataSource.getConnection();
: ----Statement statement = connection.prepareStatement(sql);
: ----ResultSet result = statement.executeQuery();
: ...
: } catch (Exception e) {
: ...
: } finally {
: ----if (result != null) {

avatar
w*z
11
you need to close all of them, connection, resultset, prepare statement.
otherwise memory leak.

you

【在 g*****g 的大作中提到】
: I think close connection is good enough. Or you can use JdbcTemplate and you
: don't need to do anything at all.

avatar
w*z
12
都这么写。写个helper会好一点。

【在 T*******x 的大作中提到】
: 据说在finally里写是最好的:
: try {
: ----Connection connection = dataSource.getConnection();
: ----Statement statement = connection.prepareStatement(sql);
: ----ResultSet result = statement.executeQuery();
: ...
: } catch (Exception e) {
: ...
: } finally {
: ----if (result != null) {

avatar
e*t
13
helper还是麻烦,等有java 8closure应该会好些吧。

【在 w**z 的大作中提到】
: 都这么写。写个helper会好一点。
avatar
g*g
14
OK, you are probably right. I haven't done raw jdbc in years. Application
server always use a connection pool and you don't really want to open a
connection every time.

【在 w**z 的大作中提到】
: you need to close all of them, connection, resultset, prepare statement.
: otherwise memory leak.
:
: you

avatar
w*z
15
java 7 added try with resource.

【在 e*****t 的大作中提到】
: helper还是麻烦,等有java 8closure应该会好些吧。
avatar
d*i
16
Spring JDBC很好,把raw JDBC的麻烦都省掉了,看到一片文章说用Spring JDBC以后甚
至连Hibernate, JPA这些ORM都不用了,简单,直接又轻便。goodbug有何高见?

you

【在 g*****g 的大作中提到】
: I think close connection is good enough. Or you can use JdbcTemplate and you
: don't need to do anything at all.

avatar
r*s
17
同意,
Criteria API真是寫的蛋疼也看得蛋疼

【在 d****i 的大作中提到】
: Spring JDBC很好,把raw JDBC的麻烦都省掉了,看到一片文章说用Spring JDBC以后甚
: 至连Hibernate, JPA这些ORM都不用了,简单,直接又轻便。goodbug有何高见?
:
: you

avatar
g*g
18
俺们开始用Spring Data,好使。常见的query都不用写了。

【在 d****i 的大作中提到】
: Spring JDBC很好,把raw JDBC的麻烦都省掉了,看到一片文章说用Spring JDBC以后甚
: 至连Hibernate, JPA这些ORM都不用了,简单,直接又轻便。goodbug有何高见?
:
: you

avatar
r*s
19
嗯,
spring data好使,
要拋棄criteria api

【在 g*****g 的大作中提到】
: 俺们开始用Spring Data,好使。常见的query都不用写了。
avatar
w*z
20
我们用 bonecp conneciton pool,performance 够好, 就是写SQL 比较蛋疼。想来
ORM 还是有用的。
有空要学习一下spring data, 我们只用Spring 的DI

【在 r*****s 的大作中提到】
: 嗯,
: spring data好使,
: 要拋棄criteria api

avatar
P*i
21
bonecp+mybatis performance很好

【在 w**z 的大作中提到】
: 我们用 bonecp conneciton pool,performance 够好, 就是写SQL 比较蛋疼。想来
: ORM 还是有用的。
: 有空要学习一下spring data, 我们只用Spring 的DI

avatar
d*i
22
这个很不错,那JdbcTemplate的那些query都可以省了?

【在 g*****g 的大作中提到】
: 俺们开始用Spring Data,好使。常见的query都不用写了。
avatar
w*z
23
我们都用stored procedure, 这还那么有用吗?

【在 d****i 的大作中提到】
: 这个很不错,那JdbcTemplate的那些query都可以省了?
avatar
w*z
24
我都人工写DAO layer, 就几个不复杂的query, 用ORM嫌麻烦,但人工写db column
mapping到 POJO, 确实烦。但一个service就几个POJO, 也不复杂。。。

【在 P****i 的大作中提到】
: bonecp+mybatis performance很好
avatar
b*y
25

en, 我也是人工写,简单。关键是,用ORM的话,你学不到底层的东东

【在 w**z 的大作中提到】
: 我都人工写DAO layer, 就几个不复杂的query, 用ORM嫌麻烦,但人工写db column
: mapping到 POJO, 确实烦。但一个service就几个POJO, 也不复杂。。。

avatar
o*i
26
我也是人工写,但是就是复制粘贴改改参数嘛,有什么底层的东东?

【在 b******y 的大作中提到】
:
: en, 我也是人工写,简单。关键是,用ORM的话,你学不到底层的东东

avatar
o*i
27
有sql server支持么?

【在 g*****g 的大作中提到】
: 俺们开始用Spring Data,好使。常见的query都不用写了。
avatar
b*y
28

en, 我没说清楚。是没啥东西了。
底层我们用的是Apache的multi-threaded object pooling library. 就是用来做jdbc
connection的那一层。

【在 o***i 的大作中提到】
: 我也是人工写,但是就是复制粘贴改改参数嘛,有什么底层的东东?
avatar
w*z
29
dbcp ?

jdbc

【在 b******y 的大作中提到】
:
: en, 我没说清楚。是没啥东西了。
: 底层我们用的是Apache的multi-threaded object pooling library. 就是用来做jdbc
: connection的那一层。

avatar
T*x
30
connection pool也需要getConnection,
用完之后也要close吧。

【在 g*****g 的大作中提到】
: OK, you are probably right. I haven't done raw jdbc in years. Application
: server always use a connection pool and you don't really want to open a
: connection every time.

avatar
w*z
31
connection pool implements close() method, it will put the connection back
to the pool. But for sure, caller needs to close ResultSet and Statement.

【在 T*******x 的大作中提到】
: connection pool也需要getConnection,
: 用完之后也要close吧。

avatar
b*y
32

Yes, it is quite good and robust. Also, simple enough

【在 w**z 的大作中提到】
: dbcp ?
:
: jdbc

avatar
g*g
33
都可以省了没有,简单的省了。不是太复杂的写个interface method足够。

【在 d****i 的大作中提到】
: 这个很不错,那JdbcTemplate的那些query都可以省了?
avatar
g*g
34
基于JPA,应该没有问题。

【在 o***i 的大作中提到】
: 有sql server支持么?
avatar
g*g
35
I don't think you need to do that when using JdbcTemplate or Spring Data/
Hibernate/JPA. You probably don't need those at all with ORM.

【在 w**z 的大作中提到】
: connection pool implements close() method, it will put the connection back
: to the pool. But for sure, caller needs to close ResultSet and Statement.

avatar
T*x
36
也需要connection.close()才能put connection back to pool吧?

【在 w**z 的大作中提到】
: connection pool implements close() method, it will put the connection back
: to the pool. But for sure, caller needs to close ResultSet and Statement.

avatar
c*e
37
re

【在 T*******x 的大作中提到】
: 也需要connection.close()才能put connection back to pool吧?
avatar
w*z
38
the connection pool implementation overrides the close method , if you call
close on the connection, it will be put back to the pool.

【在 c*********e 的大作中提到】
: re
avatar
b*y
39

call
Ya, that's about right

【在 w**z 的大作中提到】
: the connection pool implementation overrides the close method , if you call
: close on the connection, it will be put back to the pool.

avatar
m*t
40
because most of JDBC drivers use native code ( like C, or C++ ), which
create objects outside of java heap. Java GC cannot collect those native
object memory. close() method inside finally block can guarantee the
invocation of native memory release method provided by specific JDBC
provider.
avatar
w*z
41
this one is a pure Java:
http://dev.mysql.com/doc/refman/5.6/en/connector-j-overview.htm
Oralce JDBC thin driver is also written in Java
http://docs.oracle.com/cd/E16655_01/java.121/e17657/overvw.htm
Consider the following when choosing a JDBC driver for your application or
applet:
In general, unless you need OCI-specific features, such as support for
non-TCP/IP networks, use the JDBC Thin driver.
If you want maximum portability and performance, then use the JDBC Thin
driver. You can connect to Oracle Database from either an application or an
applet using the JDBC Thin driver.
If you want to use Lightweight Directory Access Protocol (LDAP) over
Secure Sockets Layer (SSL), then use the JDBC Thin driver.
If you are writing a client application for an Oracle client environment
and need OCI-driver-specific features, such as support for non-TCP/IP
networks, then use the JDBC OCI driver.
If you are writing an applet, then you must use the JDBC Thin driver.
For code that runs in the database server and needs to access a remote
database or another session within the same database instance, use the JDBC
server-side Thin driver.
If your code runs inside the database server and needs to access data
locally within the session, then use the JDBC server-side internal driver to
access that server.

【在 m*********t 的大作中提到】
: because most of JDBC drivers use native code ( like C, or C++ ), which
: create objects outside of java heap. Java GC cannot collect those native
: object memory. close() method inside finally block can guarantee the
: invocation of native memory release method provided by specific JDBC
: provider.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。