This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes database access outside of servlet container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "database access outside of servlet container" Watch "database access outside of servlet container" New topic
Author

database access outside of servlet container

Yong Bakos
Greenhorn

Joined: Jul 18, 2005
Posts: 29
In my webapp I'd like to encapsulate database access in a single class, say AppData, that classes in the model can use to populate their own fields. In addition, this would allow me to test the model classes on their own, isolated from the rest of the application.

But how would you handle database access in the AppData class to do this? For example, from within my servlet container the <database> declaration makes the datasource available to my application. But from outside the container, I have to load the driver manually with Class.forName() and use the DriverManager to get a Connection.

Is this just a tradeoff I'd have to live with? Seems there should be A Better Way...
Kerry Wilson
Ranch Hand

Joined: Oct 29, 2003
Posts: 254
This should probably be in JDBC ( read: database access outside of servlet container ).

There are many tutorials about this on the internet. Just to get you started refer to this post:

http://www.coderanch.com/t/51774/Struts/struts-database-connection

Just ignore the stuff about struts and good luck!
[ August 15, 2005: Message edited by: Kerry Wilson ]

http://www.goodercode.com
SCJP 1.4
Yong Bakos
Greenhorn

Joined: Jul 18, 2005
Posts: 29
For example.

public class AppData {

private final static DATA = new AppData();
private final static CNSTRING = "jdbc:xsql://hostname/user/pass/etc";
private DataSource datasource;

public AppData getInstance() {
return DATA;
}

private AppData() {
if (datasource == null) {
try {
// try to get a reference to the datasource via the servlet
// container's provided jndi name
Context env = (Context)new InitialContext().lookup("java:comp/env");
datasource = (DataSource)env.lookup("jdbc/mydatabase");
} catch (NamingException e) {
// handle it
}
}
}

private Connection getConnection() {
Connection cn = null;
if (datasource = null) {
// (If the datasource is null, the jndi lookup probably failed. So try
// loading the driver and getting a connection from DriverManager
try {
Class.forName("sun.jdbc.MyDatabaseJdbcDriver");
cn = DriverManager.getConnection(CNSTRING);
} catch (Exception e) {
// handle it
}
} else {
// ...otherwise just get a connection from the DataSource
try {
cn = datasource.getConnection();
} catch (Exception e) {
// handle it
}
}
return cn;
}

}

Is there a better way to handle this?

In addition, do you know if a DriverManager is actually provided by the container as well? For example, using the jndi lookup to get a datasource gives me a nice pooled datasource class provided by my servlet container, whose getConnection() method gives me a nice pooled connection. Do I only get database pooling when getting connections from a DataSource, or do I also get them via DriverManager.getConnection()?
Yong Bakos
Greenhorn

Joined: Jul 18, 2005
Posts: 29
Sorry, a little better formatting...

 
wood burning stoves
 
subject: database access outside of servlet container