How do you ppl get your connections in an EJB?
Best practice? Best performance?
-----------------
I saw this at SUN (http://java.sun.com/developer/onlineTraining/Programming/JDCBook/bmp3.html)
//Create static instance of database driver
static {
new weblogic.jdbc.jts.Driver();
}
//Get registered driver from static instance
public Connection getConnection() throws SQLException{
return DriverManager.getConnection(
"jdbc:weblogic:jts:ejbPool");
}
-----------------
It looks good and gaining good performance. But I dont like it, while it is WAS specific.
I m using the JBoss 4.0.
-
EJB and getConnection (2 messages)
- Posted by: J C
- Posted on: October 21 2004 12:55 EDT
Threaded Messages (2)
- EJB and getConnection by Kingshuk Bandyopadhyay on October 21 2004 16:58 EDT
- EJB and getConnection by J C on October 22 2004 03:46 EDT
-
EJB and getConnection[ Go to top ]
- Posted by: Kingshuk Bandyopadhyay
- Posted on: October 21 2004 16:58 EDT
- in response to J C
How do you ppl get your connections in an EJB?Best practice? Best performance?-----------------I saw this at SUN (http://java.sun.com/developer/onlineTraining/Programming/JDCBook/bmp3.html)//Create static instance of database driverstatic { new weblogic.jdbc.jts.Driver();}//Get registered driver from static instancepublic Connection getConnection() throws SQLException{ return DriverManager.getConnection( "jdbc:weblogic:jts:ejbPool");}-----------------It looks good and gaining good performance. But I dont like it, while it is WAS specific.I m using the JBoss 4.0.
Obtain it from a datasource. Look up the datasource from the jndi tree. e,g -
InitialContext ctx = new InitialContext();
javax.sql.DataSource dataSrc = (javax.sql.DataSource) ctx.lookup ("jdbc/MyDataSource");
Connection con = dataSrc.getConnection();
You will of course have to configure the datasource using your appserver configuration tools.
You get the following advantages -
1. It Perform well - since appservers usually (the ones I have seen) have a db connection pool behind the datasource
2. Code will be free from any vendor specific details - all vendor specific stuff is in the configuration step.
3. You can configure the datasource appropriate to your need s - XA connections, row pre-fetch, connection leak profiling, pool shrinking/expansion etc etc.
4. Most importantly - the db operations you do within an EJB by obtaining connections this away would be part of the ejb transaction (the container will know about it). If you obtain a connection by loading your own driver the container wouldn't "know" about it and the operations you do using that connection can not be part of a container transaction.
Reagrds
Kingshuk -
EJB and getConnection[ Go to top ]
- Posted by: J C
- Posted on: October 22 2004 03:46 EDT
- in response to Kingshuk Bandyopadhyay
Thanks alot Kingshuk!