Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JAX-WS @PostConstruct annotation and database connection  RSS feed

 
John Blunket
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a web service that uses JNDI to get the postgres connection.

At present all the web methods get call a method that gets the connection, and then at the end of the method I close the connection.



Is it better to use a method that is annotated with a @PostConstruct, this method gets the connection from the pool?





Will this even work?

John.
 
Deepika Joshi
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to have a method with @PreDestroy where you can free the reference.

PostConstruct & PreDestroy are life cycle methods, occurs only once;
PostConstrutct - after instance is created (get the resources)
PreDestroy - before instance is destroyed (free the resources)

There is no need of following thread, but still I am posting it, as I found it interesting and you may also like it,
http://forums.java.net/jive/thread.jspa?threadID=34434

thanks...
 
John Blunket
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Deepika,

When you say life cyle events, is this the lifeCycle of a webMethod call?

As far as I know, since the web service is stateless, every time I call a method from the client, it should do a PostConstruct and then after the method has finished it should do a Pre-Destroy.

Is that correct?

John.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I think you already asked this question, albeit in a slightly different form here: http://www.coderanch.com/t/466152/Web-Services/java/Glassfish-init-method
Please correct me if I am wrong.
Best wishes!
 
John Blunket
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ivan,
I think you are right.
I might just be going round the twist!!!

Thank you,
John.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!