You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Vernon Smith <vw...@lycos.com> on 2003/12/14 18:12:16 UTC

[DBCP] Usage Question

After following the TC instruction configure the DBCP with the container, I create a class to make a cleaner interface. 

public class DBCPWrapper {
	private DataSource ds;

	public DBCPWrapper() {
		try {
			Context ctx = new InitialContext();
			if (ctx == null)
				throw new Exception("Boom - No Context");
			DataSource ds =
				(DataSource) ctx.lookup("java:comp/env/jdbc/postgres");
			} catch (Exception e) {
				e.printStackTrace();
		}
	}

	public Connection getConnection() throws SQLException {
		Connection conn  = ds.getConnection(); <-- ds is NULL
		return conn;
	}
}

I don't understand why the data source is null in the getConnection method. I fix this problem by moving the body of the construtor into the getConnection method. I am not sure that is a right approach thought. Is a better way to get the thing done?

Thanks for your inputs.




____________________________________________________________
Free Poetry Contest. Win $10,000. Submit your poem @ Poetry.com!
http://ad.doubleclick.net/clk;6750922;3807821;l?http://www.poetry.com/contest/contest.asp?Suite=A59101

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


Re: [DBCP] Usage Question

Posted by Dirk Verbeeck <di...@pandora.be>.
You declared "DataSource ds" inside the constructor and that "ds" is 
being initialized and not the "private DataSource ds".

-- Dirk


Vernon Smith wrote:
> After following the TC instruction configure the DBCP with the container, I create a class to make a cleaner interface. 
> 
> public class DBCPWrapper {
> 	private DataSource ds;
> 
> 	public DBCPWrapper() {
> 		try {
> 			Context ctx = new InitialContext();
> 			if (ctx == null)
> 				throw new Exception("Boom - No Context");
> 			DataSource ds =
> 				(DataSource) ctx.lookup("java:comp/env/jdbc/postgres");
> 			} catch (Exception e) {
> 				e.printStackTrace();
> 		}
> 	}
> 
> 	public Connection getConnection() throws SQLException {
> 		Connection conn  = ds.getConnection(); <-- ds is NULL
> 		return conn;
> 	}
> }
> 
> I don't understand why the data source is null in the getConnection method. I fix this problem by moving the body of the construtor into the getConnection method. I am not sure that is a right approach thought. Is a better way to get the thing done?
> 
> Thanks for your inputs.



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