• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks in advance
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No
 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic