• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

@PostConstruct and @PreDestory

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can you please explain how @PostConstruct and @PreDestory work. I have written the following code to test, the confusion is "when i invoke this service each time postconstruct method calls before invoking my webmethod(sayHello), but PreDestroy never calls.


 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
The first sentence in the API documentation of @PostConstruct explains why your web service endpoint class' postconstruct method is called prior to handling a request:

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization.


Thus, the postconstruct method is called after dependency injection, in your case injecting the web service context, has been performed.

The predestroy method will be called if you shut down the application the web service is contained in or the entire server.
This is the time when the web service endpoint instance will be taken out of use.
Best wishes!
 
Shankar sanjay
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the Reply,

So, it would be bad (very bad idea) to use @PostConstruct method to use for any DB connection/ any resource allocation and @PreDestory method to release DB connection or releasing resources.

Am i correct?
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
No, it would not be a bad idea, as long as you are aware of the lifecycle of a webservice endpoint and consider that when selecting what to put in PostConstruct and PreDestroy methods.
Best wishes!
reply
    Bookmark Topic Watch Topic
  • New Topic