tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Evans <dsev...@berndtgroup.net>
Subject RE: Accessing resource in WEB-INF outside servlet
Date Mon, 08 Nov 2004 15:13:32 GMT
On Mon, 2004-11-08 at 09:35, Shapira, Yoav wrote:
> Hi,
> 
> >so how do you handle this? do you pass in the connection info to every
> >DAO method, like this:
> >public static Book getBook(String connection, String bookId)
> >public static void updateBook(String connection, Book book)
> >
> >that seems tedious, and since the all of my database
> 
> You can have the beans get a datasource (from a singleton possibly) and
> call its getConnection method.  To encapsulate further, you can just
> expose a getConnection method on said singleton.  That way only one
> class needs to worry about database configuration.  And you have only
> one place to change if you switch from a Class#getResource approach to a
> JNDI one or vice versa.
> 
> Yoav
> 

So the intialization of this sington datasource provider occurs the
first time the class is called? and from then on out there's only one
instance of the class, owing to its singleton nature? would that look
like this:

public class AppDBConnector {
	private static final INSTANCE = new AppDBConector();
	private static DataSource ds;

	private AppDBConnector() {
		// set static ds abov using JNDI lookup or resource bundle
	}

	public AppDBConnector getInstance() {
		return INSTANCE;
	}

	public Connection getConnection() {
		return ds.getConnection();
	}
}

Then used in the DAO like this:

Connection conn = AppDBConnector.getInstance().getConnection();





---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message