>From: "James Reynolds" <[EMAIL PROTECTED]> 
>
> Never mind, I mis-spelled the bean name in the Faces-Config. Oh the 
> shame :( 
>

The init and destroy is called twice if you navigate back to the same page 
(postback). 

http://issues.apache.org/bugzilla/show_bug.cgi?id=38000

It doesn't sound like that is what is going in your case.  I would look at 
using the JNDI connection pooling over this method if you want to use the jdbc 
api directly.  


Gary

 
> -----Original Message----- 
> From: James Reynolds [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, March 29, 2006 10:33 AM 
> To: Struts Users Mailing List 
> Subject: [Shale] When to create a database Connection? 
> 
> 
> I'm getting a null pointer error in my backing bean. My grand plan was 
> to create a Connection in the init() method extended from the 
> AbstractViewController, so it can be used by any number of methods and 
> getters in my bean, and then close it in destroy(). 
> 
> In this simple case, the only place this Connection is used is in a 
> getter that returns a Result for a dataTable. However, it doesn't seem 
> to be working as I get a null pointer exception. If I move the 
> getConnection() method out of the init() method and into this getter, 
> then everything works great. 
> 
> Am I miss-using the init() method? 
> 
> 
> Import lots.of.stuff; 
> 
> public class Testimonial extends AbstractViewController { 
> private Connection conn = null; 
> 
> public Testimonial(){ 
> } 
> 
> public void init() { 
> try { 
> conn = DbConnection.getConnection(); 
> System.out.println(); 
> } catch (SQLException e) { 
> e.printStackTrace(); 
> } 
> } 
> 
> public void destroy() { 
> try { 
> if (!conn.isClosed()) { 
> conn.close(); 
> } 
> } catch (SQLException sqle) { 
> log(sqle.getMessage(), sqle); 
> sqle.printStackTrace(); 
> } 
> } 
> 
> public Result getAllTestimonials() { 
> /* ### moving the "conn = DbConnection.getConnection()" here 
> solves the problem ### */ 
> Result result = null; 
> PreparedStatement ps = null; 
> try { 
> ps = conn.prepareStatement(SqlStatements.ALL_TESITIMONIALS); 
> result = ResultSupport.toResult(ps.executeQuery()); 
> } catch (SQLException sqle) { 
> System.out.println("SQL Exception:" + sqle.getMessage()); 
> sqle.printStackTrace(); 
> } catch (Exception e) { 
> System.out.println("Exception:" + e.getMessage()); 
> e.printStackTrace(); 
> } finally { 
> try { 
> if (ps != null) { 
> ps.close(); 
> } 
> } catch (SQLException e) { 
> e.printStackTrace(); 
> } 
> } 
> return result; 
> } 
> } 
> 
> 
> --------------------------------------------------------------------- 
> To unsubscribe, e-mail: [EMAIL PROTECTED] 
> For additional commands, e-mail: [EMAIL PROTECTED] 
> 
> 
> 
> --------------------------------------------------------------------- 
> To unsubscribe, e-mail: [EMAIL PROTECTED] 
> For additional commands, e-mail: [EMAIL PROTECTED] 
> 

Reply via email to