Menu

SqlMapClient of SingleThreadModel

rosen
2004-12-06
2013-04-11
  • rosen

    rosen - 2004-12-06

    Hi,dear friends.
    I wroten a SingleThreadModel class for SqlMapClient. like this:
    -----------------------------------------
    import java.io.Reader;

    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;

    import com.ibatis.sqlmap.client.*;
    import com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder;
    import com.ibatis.common.resources.*;

    /**
    * @author rosen jiang
    *
    */
    public class IbatisSqlMap {

        private static Reader reader;
        private static String resource = "SqlMapConfig.xml";
        private static final XmlSqlMapClientBuilder xmlBuilder =
            new XmlSqlMapClientBuilder();
        private static Logger logger =
            Logger.getLogger(IbatisSqlMap.class.getName());

        static {
            PropertyConfigurator.configure("log4j.properties");
            try {
                reader = Resources.getResourceAsReader(resource);
            } catch (Exception e) {
                logger.error(e);
            }
        }

        public static final ThreadLocal sqlMapClient = new ThreadLocal();

        /**
         *
         * @return SqlMapClient
         */
        public static SqlMapClient openSqlMapClient() {
            SqlMapClient s = (SqlMapClient) sqlMapClient.get();
            if (s == null) {
                s = xmlBuilder.buildSqlMap(reader);
                sqlMapClient.set(s);
            }
            return s;
        }
    }
    -----------------------------------------

    And when i call different DAO in JSP pages, The Exception be thrown.
    ----------------------------------------
    Caused by: com.ibatis.sqlmap.client.SqlMapException: There was an error
    while building the SqlMap instance. 
    --- The error occurred in the SQL Map Configuration file. 
    --- The error occurred while building an inline parameter map. 
    --- Cause: com.ibatis.sqlmap.client.SqlMapException: XML Parser Error.
    Cause: java.io.IOException: Stream closed
    Caused by: java.io.IOException: Stream closed
    Caused by: com.ibatis.sqlmap.client.SqlMapException: XML Parser Error.
    Cause: java.io.IOException: Stream closed
    Caused by: java.io.IOException: Stream closed
        at com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder.buildSqlMap(XmlSqlMapClientBuilder.java:198)
        at com.cdmcs.util.IbatisSqlMap.openSqlMapClient(IbatisSqlMap.java:48)
        at com.cdmcs.oa.dao.impl.TJwZbdlDaoImpl.<clinit>(TJwZbdlDaoImpl.java:27)
        ... 9 more
    ----------------------------------------

    Finally,i changed a new way,Problem solved.
    ----------------------------------------
    import java.io.Reader;

    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;

    import com.ibatis.sqlmap.client.*;
    import com.ibatis.sqlmap.engine.builder.xml.XmlSqlMapClientBuilder;
    import com.ibatis.common.resources.*;

    /**
    * @author rosen jiang
    *
    */
    public class IbatisSqlMap {

        private static Reader reader;
        private static SqlMapClient sqlMapClient;
        private static String resource = "SqlMapConfig.xml";
        private static final XmlSqlMapClientBuilder xmlBuilder =
            new XmlSqlMapClientBuilder();
        private static Logger logger =
            Logger.getLogger(IbatisSqlMap.class.getName());

        static {
            PropertyConfigurator.configure("log4j.properties");
        }

        /**
         *
         * @return SqlMapClient
         */
        public static synchronized SqlMapClient openSqlMapClient() {
            try {
                reader = Resources.getResourceAsReader(resource);
                sqlMapClient=xmlBuilder.buildSqlMap(reader);
            } catch (Exception e) {
                logger.error(e);
            }
            return sqlMapClient;
        }
    }
    ----------------------------------------

    But, I wonder what's the matter.
    Only one SqlMap instance be allowed here?

     
    • Brandon Goodin

      Brandon Goodin - 2004-12-06

      SqlMap is already Thread safe. It uses ThreadLocal internally. So, all you need to do is persist a single instance of it that is accessible from your DAOs. You could use the IBatis DAO framework or Spring to accomplish the persistence of your SQLMap. The other well known way is to have a base DAO class that all your DAOs extend and persist a static instance of the SQLMap on the base class. So, this line is not required:

      public static final ThreadLocal sqlMapClient = new ThreadLocal();

      Also, I'm not sure why you have the method openSqlMapClient. If you are spinning your own SqlMap loader class you should really accomplish all of that in a static block and use the DAO pattern that I shared above to access the SqlMapClient. I would recommend that you use IBatis DAO or Spring to accomplish that though.

      Brandon

      P.S. Please post further questions to the Apache mailing list.

      subscribe here:
      ibatis-user-java-subscribe@incubator.apache.org

       
    • rosen

      rosen - 2004-12-07

      After read iBATIS-DAO-2 PDF,I knew lots of things.

      Thanks Brandon !

       

Log in to post a comment.