• 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
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

How to get Connection instance

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 3902
10
Redhat Quarkus Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic