Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Glassfish init method  RSS feed

 
John Blunket
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am writing a web service, and I need to connect to my database when I start a new session.

What methods do I need to implement at session start, and end?
What are thier parameters?


Regards,
John.
 
John Blunket
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Blunket wrote:Hi,
I am writing a web service, and I need to connect to my database when I start a new session.

What methods do I need to implement at session start, and end?
What are thier parameters?


Regards,
John.


Ok,
I think I need to use @Resource @PostConstruct annotations?
John
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Do you mean a session with a client or when the web service starts up?
If the latter, then you are correct about the annotations. If the former, then it is a little more complicated - I'll give it a try if you need it.
What kind of web service endpoint implementation class are you using; a POJO or an EJB?
Best wishes!
 
John Blunket
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan,
Thank you for the reply.
Now I am a little bit more confused.
If I explain my application, maybe you could comment.

The web application I have is using a simple driver based JDBC interface. So verey webmethod copens a connection and closes it again.

I now want to move over to using JNDI and a connection pool.

So I thought, that with evey web method, I would get the connection, use it, and then close it.

Instead of each method having the same code to get the connection, I would use a method with the @PostConstruct to get my connection from the pool, and then PreDestroy to close the connection.

is that not a good idea?

Whats the best method for this?

I am using a POJO implementation.

Regards,
John.

 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
You probably want to inject a DataSource using the @Resource annotation and then get, and eventually return, connections from this DataSource.
Note that the injection of the DataSource is done only when the instance of the endpoint implementation class is created, but you can retrieve database connections from it each time there is a request to serve.
For more information, see the Java EE 5 Tutorial: http://java.sun.com/javaee/5/docs/tutorial/doc/bncjj.html
Best wishes!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!