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

How to get Connection instance  RSS feed

 
Niu Xiuyuan
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my application server is AppServer5.0,I wanna write a entity bean which maintained by bean.But I don't know how to get the Connection instance in ejbCreate() method.
 
bin jian
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
according to "maintained by bean",i guess that you means BMP,if then,you can first set up your own connection pool in application server,and get one connection per time via jndi name.i write my codes like the following:

private Connection getConnection() throws SQLException {
try
{
Context jndiCntx = new InitialContext();
DataSource ds =(DataSource)jndiCntx.lookup("java:/mySQLDS");
return ds.getConnection();
}
catch (NamingException ne)
{
throw new EJBException(ne);
}
}
besides,make sure that connection is released after used it per time,for example,in ejbCreate() or ejbStore(),before quiting methods remember to invoke close() on connection.
hope this will be helpful!
[ January 10, 2002: Message edited by: bin jian ]
[ January 10, 2002: Message edited by: bin jian ]
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by "connection maintained by bean" you mean you'd create the connection in EJB create, please DON'T DO THAT! ejbCreate() is called once in the lifecycle of the bean -- if you do a finder method to get the bean it does NOT call EJB create()!
In general DON'T try to cache the Connection in the bean period. Just use the kind of code posted above and grab a new connection each time you need one. The application server itself handles connection pooling.
Kyle
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!