Menu

iBatis db layer + struts

2003-07-02
2003-07-03
  • Nobody/Anonymous

    Basically I have made the ibatis and struts examples working, and I can build some simple apps with both.  However, I do not really know how to link both of them together.  The current solution (I think is rather
    stupid) is to get an instance of the sqlMap by calling the sqlMap =
    SqlMapConfigExample.getConfiguredSqlMap()  (quoted from the ibatis example), and then use that instance in doing other action like delete, or returning lists for display in the view (jsp files). 

    This is done by action classes creating an instance of a special class e.g. DBAccess when the action class is initialized. So,

    Jsp --> control --> action --(init)instantiate--> DBAccess
    --instantiate--> sqlMap --> database --> List of beans --> jsp (display)

    However, can I have a single instance of sqlMap for a session?  How can I do it?  An example would be a very big step for me. 

    Thanks, and sorry for the English as well as very possible use of bad keywords...

    Andy

     
    • Clinton Begin

      Clinton Begin - 2003-07-02

      Hi Andy,

      Getting the SqlMap from a centralized singleton (like SqlMapConfigExample) isn't bad (i.e. not stupid), although you'd probably want to get rid of the word 'Example'.  ;-)

      It's actually a useful pattern to centralize configuration into a neat little object like that.  Note that it is not instantiated every time -- but only once.  It's initialized in a static {block} when the class is loaded.

      Another way to do it is to initialize it in a base class, like BaseLogic, or BaseAction --depending on your layering strategy.  If you'd like an example of the latter, check out the JPetStore example.  Although I'm going to eventually use the singleton configuration class in future JP versions.

      Cheers,
      Clinton

       
    • Nobody/Anonymous

      Thanks...  one more thing I am worrying is the connection to the database.  If I am using this centralised singleton design, would the connection open and close everytime?  Would that be slow if I need to run many nested queries?

      Thanks again!

       
    • Clinton Begin

      Clinton Begin - 2003-07-03

      Hi Andy,

      Both the SQL Map and DAO frameworks implement connection pooling.  You have your choice of iBATIS SimpleDataSource, Jakarta DBCP or your app server's DataSource implementation (via JNDI).

      So the answer is "no", connections won't open and close every time. 

      Cheers,
      Clinton

       
    • Nobody/Anonymous

      I use Struts + ibatis with good results.

      Make a Struts Plugin and store the location og the ibatis config file in struts-config.xml.

      When Struts starts up and loads it's Plugins, the ibatis plugin is given the configfile name. From there initialize as you already do.

      /Per
      per@net-es.dk

       
    • Nobody/Anonymous

      Thanks for the suggestions, but I currently have the problem below, which I do not know why it happens.  It says the setting element is not declared, but I am using the dtd file from http://www.ibatis.com/dtd/sql-map-config.dtd.  I have the settings element in my sqlconfig.xml file:

        <settings
           maxExecute="0"
           maxExecutePerConnection="0"
           maxTransactions="5"
           statementCacheSize="0"
           globalTransactions="false"
         />

      From the limited XML knowledge (actually none) I can see there is no problem.

      I have the following error:

      com.ibatis.db.sqlmap.SqlMapException: Error while building SqlMap. 
      Cause:
      org.jdom.JDOMException: Error on line 22 of document : Element type "settings" must be declared.
              at com.ibatis.db.sqlmap.XmlSqlMapBuilder.buildSqlMap(Unknown Source)
              at domain.mydomain.sqlmap.SqlMapConfig.getConfiguredSqlMap(SqlMapConfig.java:22)
              at domain.mydomain.sqlmap.TestSqlMapConfig.main(TestSqlMapConfig.java:31)
      Exception in thread "main"

       
    • Nobody/Anonymous

      When i try to validate the SqlMapConfig.xml I have the following error:

      Attribute "globalTransactions" must be declared for element type "settings". [26]

      BUT as you can see from the above message, I have that setting.  What should I do?

       
    • Clinton Begin

      Clinton Begin - 2003-07-03

      Two things:

      1) The name "globalTransactions" has been changed to "useGlobalTransactions".  There was a bug regarding this.  Please ensure to use 1.2.5 RC1.  This will fix the naming problem, and also allow you to specify only the settings you need.

      2) If you are using an IDE that caches DTDs (such as InteliJ IDEA), be sure to refresh the DTD from the server (in IntelliJ you have to delete the resource and recreate it).

      I hope that helps.

      Cheers,
      Clinton

       

Log in to post a comment.