ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alan Chandler <a...@chandlerfamily.org.uk>
Subject Re: best method to get SqlMapClient in web application
Date Tue, 25 Oct 2005 10:51:18 GMT
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 

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 = 
	      Reader reader = Resources.getResourceAsReader (resource);
	      sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	   } catch (Exception e) {
	      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 {

Alan Chandler
Open Source. It's the difference between trust and antitrust.

View raw message