ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Larry Meadors <lmead...@apache.org>
Subject Re: best method to get SqlMapClient in web application
Date Tue, 25 Oct 2005 15:13:30 GMT
The other issue with statics is that they are virtually impossible to
mock for testing.

One pattern I have used was to put references to my dao classes on my
business logic classes. I then created two constructors - one without
args, and one with the dao instances as parameters. The no-arg
constructor pulled the dao classes from the dao manager, and the other
injected them. That way, when I tested, I could plug in different dao
impls.

Spring makes this TONS easier though.

Larry


On 10/25/05, Jeff Butler <jeffgbutler@gmail.com> wrote:
> I would say it's not *exactly* singleton, but close enough.  And it
> certainly works.  As to the question "why not this?", I have these thoughts:
>
> <joking>It's not cool as cool as the other ways, and we're all about
> cool</joking>
>
> On a more serious note, I do try to avoid Singletons and static
> methods/variables if possible.  Sometimes it's not possible, but if there is
> a simple alternative I will take it.  I think the best reason I can give for
> this philosophy is that I want to be in more explicit control over when
> initialization occurs.  Static variables are initialized at first class
> reference, and it's not always easy to predict when that will be.
>
> But, of course, this is more philosophy than science.  Sometimes in our
> efforts to do something elegant, we make things too complex.  There can be a
> trade off between simple and elegant - your milage may vary.  If it works,
> and is maintainable, and you are happy with it, then I say "go for it".
>
> Jeff Butler
>
>
>
> On 10/25/05, Ashish Kulkarni <kulkarni_ash1312@yahoo.com> wrote:
> > Alan
> > What you are doing is singleton pattern, it makes sure
> > that there is only one instance of SqlMapClient
> > through out your application.
> >
> > Ashish
> >
> >
> >
> > --- Alan Chandler <alan@chandlerfamily.org.uk> wrote:
> >
> > > On Monday 24 Oct 2005 16:50, Ashish Kulkarni wrote:
> > > > Hello
> > > > What is the best way to get SqlMapClient in a web
> > > > application
> > > > 1 , Define a singleton pattern class, and create a
> > > > instance of SqlMapClient to use by all classes
> > > which
> > > > need to access data.
> > > >
> > > > 2, Define a AbstractClass and put method to get
> > > > SqlMapClient in constructor of this class, and
> > > then
> > > > each class which needs to get data extends this
> > > class
> > > >
> > > >
> > > > 3, Create Instance of SqlMapClient in one of the
> > > init
> > > > servlets, and store this SqlMapClient in
> > > > ServletContext and pass it as one of the
> > > parameters to
> > > > all data classes
> > > >
> > > > 4, or any other method
> > >
> > > I have been following this thread without really
> > > understanding it.
> > >
> > > I would be interested in any comments on what I do,
> > > which I think is point 1
> > > (but I am new to Java and I am not sure I understand
> > > what a singleton is
> > > properly).  In particular, I am not sure I
> > > understand what the potential
> > > Gotcha's are.  Why doesn't everybody do it this way,
> > > it seems so SIMPLE.
> > >
> > > The reason I do this is that I am attempting to
> > > avoid creating a session to
> > > early in my application - but this has not been
> > > commented on in the thread at
> > > all.
> > >
> > > This is my basic class:
> > >
> > > package uk.org.chandlerfamily.sqlmap.famtree;
> > >
> > > import com.ibatis.common.resources.Resources;
> > > import com.ibatis.sqlmap.client.*;
> > > import java.io.Reader;
> > >
> > > public class FamtreeMap {
> > >       private static final SqlMapClient sqlMap;
> > >       static {
> > >         try {
> > >             String resource =
> > >
> > "uk/org/chandlerfamily/sqlmap/famtree/SqlMapConfig.xml";
> > >             Reader reader = Resources.getResourceAsReader
> > > (resource);
> > >             sqlMap =
> > > SqlMapClientBuilder.buildSqlMapClient(reader);
> > >          } catch (Exception e) {
> > >             e.printStackTrace();
> > >             throw new RuntimeException ("Error
> > > initializing DataMap class. Cause: "
> > > + e);
> > >          }
> > >       }
> > >       public static SqlMapClient getSqlMapInstance () {
> > >         return sqlMap;
> > >       }
> > >
> > > }
> > >
> > >
> > >
> > > and then whenever I want to do something with my
> > > database I do
> > >
> > >               SqlMapClient map=FamtreeMap.getSqlMapInstance ();
> > >               try {
> > >                       map.startTransaction();
> > > ...
> > > ...
> > >
> > > --
> > > Alan Chandler
> > > http://www.chandlerfamily.org.uk
> > > Open Source. It's the difference between trust and
> > > antitrust.
> > >
> >
> >
> >
> >
> > __________________________________
> > Yahoo! FareChase: Search multiple travel sites in one click.
> > http://farechase.yahoo.com
> >
>
>

Mime
View raw message