ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeff Butler <jeffgbut...@gmail.com>
Subject Re: best method to get SqlMapClient in web application
Date Tue, 25 Oct 2005 15:00:31 GMT
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