struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Alexandre Poitras" <alexandre.poit...@gmail.com>
Subject Re: [Shale] When to create a database Connection?
Date Thu, 30 Mar 2006 11:40:23 GMT
If you need to finish this project quickly, Spring JDBCTemplate is
your friend. It will take care of this messy exception handling code
for you. Trust me, it doesn't take more then 1 or 2 hours to learn it
and you will save tons of time. Plus, it makes your code easier to
migrate to a ORM tool if you have to in the future.

On 3/29/06, Gary VanMatre <gvanmatre@comcast.net> wrote:
> >From: "James Reynolds" <James.Reynolds@intermountainmail.org>
> >
> > 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:James.Reynolds@intermountainmail.org]
> > 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: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> > For additional commands, e-mail: user-help@struts.apache.org
> >
>


--
Alexandre Poitras
Qu├ębec, Canada

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


Mime
View raw message