You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Anoop kumar V <an...@gmail.com> on 2009/05/24 05:08:08 UTC

Best practice to configure SQL Map for a web application

I was reading the example that ships with Ibatis and the initializer block
has this comment before configuring the SQL Map. I was just curious what is
the best practice of configuring one if not in the class initializer / a
constructor? I was hoping to call the class as part of the servlet init.

  /**
   * It's not a good idea to put code that can fail in a class initializer,
   * but for sake of argument, here's how you configure an SQL Map.
   */
  static {
    try {
      Reader reader =
Resources.getResourceAsReader("com/mydomain/data/SqlMapConfig.xml");
      sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
      reader.close();
    } catch (IOException e) {
      // Fail fast.
      throw new RuntimeException("Something bad happened while building the
SqlMapClient instance." + e, e);
    }


Thanks,
Anoop

Re: Best practice to configure SQL Map for a web application

Posted by Clinton Begin <cl...@gmail.com>.
You can use a factory, a singleton, or a dependency injection container such
as Spring or Guice.

Cheers,
Clinton

On Sat, May 23, 2009 at 9:08 PM, Anoop kumar V <an...@gmail.com>wrote:

> I was reading the example that ships with Ibatis and the initializer block
> has this comment before configuring the SQL Map. I was just curious what is
> the best practice of configuring one if not in the class initializer / a
> constructor? I was hoping to call the class as part of the servlet init.
>
>   /**
>    * It's not a good idea to put code that can fail in a class initializer,
>    * but for sake of argument, here's how you configure an SQL Map.
>    */
>   static {
>     try {
>       Reader reader =
> Resources.getResourceAsReader("com/mydomain/data/SqlMapConfig.xml");
>       sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
>       reader.close();
>     } catch (IOException e) {
>       // Fail fast.
>       throw new RuntimeException("Something bad happened while building the
> SqlMapClient instance." + e, e);
>     }
>
>
> Thanks,
> Anoop
>