Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

does gc still have a chance to do its job if i kill the java process

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks in advance
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No
 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
What are you doing in my house? Get 'em tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic