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 Hycel Taylor <hy...@gmail.com> on 2006/11/22 21:09:55 UTC

Need a custom way for sqlMapConfig.xml to get JDBC properties

In our environment, we are currently using iBatis in three separate
databases.  We deploy to four environments, dev, staging, and two
production environments.  For security reasons, the database user
names and passwords are different in each environment.  Deploying jars
that use iBatis is cumbersome because the sqlMapConfig.xml has to live
in the jar and the properties file which the sqlMapConfig references
must also exist within the class path.

Example:

<sqlMapConfig>
  <properties resource="config/feeddb/ibatis/feeddb.properties"/>

  <transactionManager type="JDBC">
    <dataSource type="DBCP">
      <property name="JDBC.Driver" value="${db.connectionDriver}"/>
      <property name="JDBC.ConnectionURL" value="${db.connectionURL}"/>
      <property name="JDBC.Username" value="${db.connectionUserName}"/>
      <property name="JDBC.Password" value="${db.connectionPassword}"/>
      <property name="JDBC.DefaultAutoCommit" value="false"/>
      <property name="Pool.MaximumActiveConnection" value="10"/>
      <property name="Pool.MaximumIdleConnections" value="5"/>
      <property name="Pool.MaximumCheckoutTime" value="60000"/>
      <property name="Pool.TimeToWait" value=""/>
    </dataSource>
  </transactionManager>
…

I've been given the mandate to "Change all configuration files used by
iBatis to be read from one central place".   In order to meet this
goal, I need to modify, hopefully, only a few iBatis java classes that
would use the interface, shown below, to in order to retrieve JDBC
property parameters.

public interface CustomResource {
  public String getConnectionDriver();
  public String getConnectionURL();
  public String getConnectionUserName();
  public String getConnectionPassword();
}

I could then write a concrete class that implements CustomResource to
retrieve JDBC resource information anyway I want.  There are a few
more things that I need to hammer out in order to implement this.

1) What iBatis class or classes would I need to modify to use the
CustomResource intereface?

2) In sqlMapConfig.xml I would also need to define a property to
specify the use of the custom class.

Example: <property
customResource="com.test.app.feeddb.dataAccess.ibatis.CustomResourceImpl"/>

Now, it seems that I may have to modify some more iBatis classes and
the DTD to make this possible.

I would appreciate any advice, suggestions or other possible solutions
to resolve this issue.

Thank you,

Hycel Taylor
Principal Architect
80108 Media

Re: Need a custom way for sqlMapConfig.xml to get JDBC properties

Posted by Hycel Taylor <hy...@gmail.com>.
We don't use any containers.  However, we do use Spring with ActiveMQ.
 Spring might be a possible solution.

Thank you for your suggestions.

Re: Need a custom way for sqlMapConfig.xml to get JDBC properties

Posted by Chris Lamey <cl...@localmatters.com>.
Just a suggestion:  You could configure iBATIS to use JNDI and then
either use your container's JNDI provider or use a standalone one.

I'd also ask if the cost of writing your own iBATIS properties loading
code is cheaper than copying the right properties file into the right
CLASSPATH.  That doesn't seem like a good use of developer time to me
and not fun to maintain, especially if the iBATIS guys change things out
from under you.  Most environments manage their differences by copying
around the same war/ear/jar and then some kind of external
configuration.

Another idea would be to use Spring, which has all kinds of ways to do
this stuff.

On Wed, 2006-11-22 at 15:09 -0500, Hycel Taylor wrote:
> In our environment, we are currently using iBatis in three separate
> databases.  We deploy to four environments, dev, staging, and two
> production environments.  For security reasons, the database user
> names and passwords are different in each environment.  Deploying jars
> that use iBatis is cumbersome because the sqlMapConfig.xml has to live
> in the jar and the properties file which the sqlMapConfig references
> must also exist within the class path.
> 
> Example:
> 
> <sqlMapConfig>
>   <properties resource="config/feeddb/ibatis/feeddb.properties"/>
> 
>   <transactionManager type="JDBC">
>     <dataSource type="DBCP">
>       <property name="JDBC.Driver" value="${db.connectionDriver}"/>
>       <property name="JDBC.ConnectionURL" value="${db.connectionURL}"/>
>       <property name="JDBC.Username" value="${db.connectionUserName}"/>
>       <property name="JDBC.Password" value="${db.connectionPassword}"/>
>       <property name="JDBC.DefaultAutoCommit" value="false"/>
>       <property name="Pool.MaximumActiveConnection" value="10"/>
>       <property name="Pool.MaximumIdleConnections" value="5"/>
>       <property name="Pool.MaximumCheckoutTime" value="60000"/>
>       <property name="Pool.TimeToWait" value=""/>
>     </dataSource>
>   </transactionManager>
> …
> 
> I've been given the mandate to "Change all configuration files used by
> iBatis to be read from one central place".   In order to meet this
> goal, I need to modify, hopefully, only a few iBatis java classes that
> would use the interface, shown below, to in order to retrieve JDBC
> property parameters.
> 
> public interface CustomResource {
>   public String getConnectionDriver();
>   public String getConnectionURL();
>   public String getConnectionUserName();
>   public String getConnectionPassword();
> }
> 
> I could then write a concrete class that implements CustomResource to
> retrieve JDBC resource information anyway I want.  There are a few
> more things that I need to hammer out in order to implement this.
> 
> 1) What iBatis class or classes would I need to modify to use the
> CustomResource intereface?
> 
> 2) In sqlMapConfig.xml I would also need to define a property to
> specify the use of the custom class.
> 
> Example: <property
> customResource="com.test.app.feeddb.dataAccess.ibatis.CustomResourceImpl"/>
> 
> Now, it seems that I may have to modify some more iBatis classes and
> the DTD to make this possible.
> 
> I would appreciate any advice, suggestions or other possible solutions
> to resolve this issue.
> 
> Thank you,
> 
> Hycel Taylor
> Principal Architect
> 80108 Media

Re: Need a custom way for sqlMapConfig.xml to get JDBC properties

Posted by Hycel Taylor <hy...@gmail.com>.
Hey Julien!

I tried out your suggestion.  That's that ticket!  It works beautifully.

Thanks for the great advice!

Re: Need a custom way for sqlMapConfig.xml to get JDBC properties

Posted by Julien Lafontaine <ju...@gmail.com>.
I used to use the DaoManager as well and it worked fine. You have to use the
method buildDaoManager  that takes a Properties as parameter in the
DaoManagerBuilder class. You can create one daoManager per database and then
get you dao through those managers.

The dao manager is going to forward those parameters to the sqlMap instance
that it creates.

I am talking about iBatis Dao here. I am not tu sure about Spring.

Hope this helps

Julien



On 11/24/06, Hycel Taylor <hy...@gmail.com> wrote:
>
> Hey!  This sounds like a pretty good idea.  Another question however.
> We're using the DAO manager.  Will this cause a problem?  Will the DAO
> manager allow access to the sqlMap so that properties can be set
> before a connection to the database is made?
>
> Thank you for the suggestion.
>

Re: Need a custom way for sqlMapConfig.xml to get JDBC properties

Posted by Hycel Taylor <hy...@gmail.com>.
Hey!  This sounds like a pretty good idea.  Another question however.
We're using the DAO manager.  Will this cause a problem?  Will the DAO
manager allow access to the sqlMap so that properties can be set
before a connection to the database is made?

Thank you for the suggestion.

Re: Need a custom way for sqlMapConfig.xml to get JDBC properties

Posted by Julien Lafontaine <ju...@gmail.com>.
Why don't you just do something like that:


Properties p = new Properties();
p.setProperty("db.connectionDriver", "driver");

p.setProperty("db.connectionUserName", "driver");

...
SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader, p);

It worked for me

Julien




On 11/23/06, Hycel Taylor <hy...@gmail.com> wrote:
>
> In our environment, we are currently using iBatis in three separate
> databases.  We deploy to four environments, dev, staging, and two
> production environments.  For security reasons, the database user
> names and passwords are different in each environment.  Deploying jars
> that use iBatis is cumbersome because the sqlMapConfig.xml has to live
> in the jar and the properties file which the sqlMapConfig references
> must also exist within the class path.
>
> Example:
>
> <sqlMapConfig>
> <properties resource="config/feeddb/ibatis/feeddb.properties"/>
>
> <transactionManager type="JDBC">
>    <dataSource type="DBCP">
>      <property name="JDBC.Driver" value="${db.connectionDriver}"/>
>      <property name="JDBC.ConnectionURL" value="${db.connectionURL}"/>
>      <property name="JDBC.Username" value="${db.connectionUserName}"/>
>      <property name="JDBC.Password" value="${db.connectionPassword}"/>
>      <property name="JDBC.DefaultAutoCommit" value="false"/>
>      <property name="Pool.MaximumActiveConnection" value="10"/>
>      <property name="Pool.MaximumIdleConnections" value="5"/>
>      <property name="Pool.MaximumCheckoutTime" value="60000"/>
>      <property name="Pool.TimeToWait" value=""/>
>    </dataSource>
> </transactionManager>
> …
>
> I've been given the mandate to "Change all configuration files used by
> iBatis to be read from one central place".   In order to meet this
> goal, I need to modify, hopefully, only a few iBatis java classes that
> would use the interface, shown below, to in order to retrieve JDBC
> property parameters.
>
> public interface CustomResource {
> public String getConnectionDriver();
> public String getConnectionURL();
> public String getConnectionUserName();
> public String getConnectionPassword();
> }
>
> I could then write a concrete class that implements CustomResource to
> retrieve JDBC resource information anyway I want.  There are a few
> more things that I need to hammer out in order to implement this.
>
> 1) What iBatis class or classes would I need to modify to use the
> CustomResource intereface?
>
> 2) In sqlMapConfig.xml I would also need to define a property to
> specify the use of the custom class.
>
> Example: <property
> customResource="com.test.app.feeddb.dataAccess.ibatis.CustomResourceImpl
> "/>
>
> Now, it seems that I may have to modify some more iBatis classes and
> the DTD to make this possible.
>
> I would appreciate any advice, suggestions or other possible solutions
> to resolve this issue.
>
> Thank you,
>
> Hycel Taylor
> Principal Architect
> 80108 Media
>