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 Mon, 24 Oct 2005 19:33:16 GMT
Sure - the singleton works fine. But you don't have to make the GetData
class dependant on the servlet. You could pass the SqlMapClient as a
parameter to the methods in your GetData class (or in a constructor) - then
there's no Servlet dependency
 Another thing to look at...iBATIS DAOs. Write your GetData class as an
iBATIS DAO. Then put a DAOManager in the servlet context. Your
servlets/actions/etc. could pull the DaoManager out of the context and
retrieve the DAO from the DaoManager. Everything is nicely encapsulated and
there are no unusual dependencies. This is what I always do if there is no
EJB container involved (with an EJB container, I would put the DAOManager in
a Singleton).
 Jeff Butler

 On 10/24/05, Ashish Kulkarni <kulkarni_ash1312@yahoo.com> wrote:
>
> Jeff
> Suppose if i have a following class
> GetData, which has following code
> sqlMapClient.delete("deleteCOExternal",coDataBean);
>
> This GetData class is being called by a servlet, then
> this servlet has to get sqlMapClient from servlet
> context and pass it to GetData class,
> this way we are tying GetData with servlet, If i have
> to call GetData without using servlet then i will have
> to design my own ServletContext type of setup.
>
> Have you ever tried to user singleton pattern to get
> sqlMapClient,
> and then in GetData class do
> GetMySqlMap.getInstance().getSqlMap()
> This way we dont have class GetData depend on servlet.
>
> Ashish
>
>
>
> --- Jeff Butler <jeffgbutler@gmail.com> wrote:
>
> > Here's a listener:
> > package somepackage;
> >
> > import javax.servlet.ServletContextEvent ;
> > import javax.servlet.ServletContextListener;
> >
> > public class ContextListener implements
> > ServletContextListener {
> >
> > public ContextListener() {
> > super();
> > }
> > public void contextInitialized(ServletContextEvent
> > sce) {
> > // generate your client through normal methods
> > SqlMapClient client = createSqlMapClient();
> >
> >
> sce.getServletContext().setAttribute(SqlMapClient.class.getName(),
> > client);
> > }
> > public void contextDestroyed(ServletContextEvent
> > sce) {
> > }
> > }
> > Then you need to write a method somewhere like
> > this:
> > public SqlMapClient getSqlMapClient(ServletContext
> > context) {
> > return (SqlMapClient)
> > context.getAttribute(SqlMapClient.class.getName());
> > }
> > You also need to configure the listener in web.xml.
> > Pretty simple...
> > Jeff Butler
> >
> > On 10/24/05, Ashish Kulkarni
> > <kulkarni_ash1312@yahoo.com> wrote:
> > >
> > > Jeff
> > > Can you provide me a sample code of putting
> > > SqlMapClient in servlet context with listener and
> > > access it from class which requires to access it
> > >
> > > Ashish
> > >
> > > --- Jeff Butler < jeffgbutler@gmail.com> wrote:
> > >
> > > > I don't know if it's the best or not, but I
> > often
> > > > times put the SqlMapClient
> > > > into the servlet context with a listener. I
> > think
> > > > this is essentially what
> > > > Spring does too.
> > > > Jeff Butler
> > > >
> > > > On 10/24/05, Ashish Kulkarni
> > > > < kulkarni_ash1312@yahoo.com> 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
> > > > >
> > > > > Ashish
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > __________________________________
> > > > > Yahoo! Mail - PC Magazine Editors' Choice 2005
> > > > > http://mail.yahoo.com
> > > > >
> > > >
> > >
> > >
> > >
> > >
> > > __________________________________
> > > Start your day with Yahoo! - Make it your home
> > page!
> > > http://www.yahoo.com/r/hs
> > >
> >
>
>
>
>
>
> __________________________________
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> http://mail.yahoo.com
>

Mime
View raw message