• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

database access outside of servlet container

 
Yong Bakos
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Yong Bakos
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, a little better formatting...

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic