| Author |
does gc still have a chance to do its job if i kill the java process
|
zheng li
Ranch Hand
Joined: Jun 16, 2009
Posts: 56
|
|
|
thanks in advance
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
No
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
zheng li
Ranch Hand
Joined: Jun 16, 2009
Posts: 56
|
|
Wouter Oet wrote:No
actually, i am using SimpleDataSource class in ibatis, and it releases all database connections in its finalize method.
i'm surprised that all database connections disappear even if i kill the java process.
i'm not sure when and how it does the job. so i asked the question.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
It is not recommended to close connections in finalizers because there is no guaranty that it will run.
Try adding a shutdownhook to the runtime.
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
When you "kill" the java process and the DataSource connections are closed, it doesn't mean that the finalize method was called. And as Wouter said, there are no guarantees that finalize method will run, you should think about redesigning your logic so that you close the connection after your use is over...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
zheng li
Ranch Hand
Joined: Jun 16, 2009
Posts: 56
|
|
sorry that i didn't make my question clear.
I know there are no guarantees that finalize method will run, so I don' understand why when i killed the java process, all database connections disappeared.
i don't think SimpleDataSource class has the chance to close the connections.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16692
|
|
zheng li wrote:
I know there are no guarantees that finalize method will run, so I don' understand why when i killed the java process, all database connections disappeared.
i don't think SimpleDataSource class has the chance to close the connections.
When a process terminates, the operating system frees all resources.
There is no need to run the GC because all allocated memory will be freed. There is no need to finalize the database connection, because the OS will close all files and network connections.
This is done for all applications, regardless of whether it is the JVM or not.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
zheng li
Ranch Hand
Joined: Jun 16, 2009
Posts: 56
|
|
Henry Wong wrote:
zheng li wrote:
I know there are no guarantees that finalize method will run, so I don' understand why when i killed the java process, all database connections disappeared.
i don't think SimpleDataSource class has the chance to close the connections.
When a process terminates, the operating system frees all resources.
There is no need to run the GC because all allocated memory will be freed. There is no need to finalize the database connection, because the OS will close all files and network connections.
This is done for all applications, regardless of whether it is the JVM or not.
Henry
thank you.
i met something on windows like after i terminate a program, i can't delete the file the program has modified. so i always thought that i couldn' rely on OS.
but now it seems OS does some cleanup for us.
|
 |
 |
|
|
subject: does gc still have a chance to do its job if i kill the java process
|
|
|