ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Lamey <cla...@localmatters.com>
Subject Re: How to setup JNDI connection in ibatis
Date Thu, 27 Sep 2007 16:46:05 GMT
Well, it's really a Tomcat configuration issue, not an iBATIS one.

But if people think it's worthwhile, here's a shot at for the iBATIS FAQ.
I'm embarrassed to say I don't know how to add things to the FAQ...

Q : How do I use a JNDI DataSource with iBATIS in Tomcat?

A : This breaks down into two logical parts.  First you have to declare the
DataSource in JNDI in Tomcat.  Then you have to tell iBATIS to use that
object via its JNDI name.

Putting a DataSource into JNDI in Tomcat depends on the version of Tomcat
you have.  We'll use Tomcat 5.5 as an example here.  Take a look at the
Tomcat docs for specifics on your version or more details in general.

You declare JNDI DataSource objects in Tomcat using the Resource xml
element.  Here's an example:

<Resource
      name="jdbc/AS400B"
      type="javax.sql.DataSource"
      password="password"
      driverClassName="com.ibm.as400.access.AS400JDBCDriver"
      maxIdle="2"
      maxWait="5000"
      validationQuery="select * from PODATA"
      username="userid"
      url="jdbc:as400:AS400B/RPTSTEXTDB;naming=system;date format=iso;time
format=hms;prompt=false"
      maxActive="4"/>

The Resource element either goes in your global JNDI context or in a webapp
specific context.  To put it in your global JNDI context, put your
DataSource Resource element as a child of the GlobalNamingResources element
in conf/server.xml.  If you put it in the global JNDI context, you need to
use a ResourceLink in either your webapp's context or in the
conf/context.xml (which is shared by all webapps).  Here's a ResourceLink
example:

    <ResourceLink name="jdbc/AS400B"
        global="jdbc/AS400B"
        type="javax.sql.DataSource"/>

To put it in a webapp specific context, depends on how your webapps are set
up. If you have a context file for your webapp, you can put the Resource as
a child of the Context element.  If you don't have a context file for you
webapp, you can put the Resource element as a child of the webapp's Context
element in conf/server.xml.

Another place for the Resource element could be in the conf/context.xml
file, which is shared by all webapps.

Once the DataSource is bound in JNDI, you need to tell iBATIS to use it.
NOTE: that Tomcat helpfully prepends the string "java:/comp/env/" to your
names, which is needed to do the lookups.

If you're using the iBATIS sqlmap xml config files, you need this:

 <transactionManager type="JDBC">
        <dataSource type="JNDI">
               <property name="DataSource"
value="java:/comp/env/jdbc/AS400B"/>
            </dataSource>
   </transactionManager>

If you're using Spring, you need to reference the JNDI DataSource object as
a JndiObjectFactoryBean and then set that as the DataSource on your
SqlMapClientFactoryBean.  Here's an example:

    <bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:/comp/env/jdbc/AS400B</value>
        </property>
    </bean>

    <bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation"
value="classpath:nontology/sqlmap-config.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

Cheers,
Chris

On 9/27/07 10:23 AM, "Ashish Kulkarni" <ashish.kulkarni13@gmail.com> wrote:

> Hi
> Thank you very much, i changed the parameter in SqlMapConfig.xml to
> java:/comp/env/jdbc/AS400B and it works now
> I wish this was documented some where in ibatis, or it is and i have never
> read it
> 
> Ashish
> On 9/27/07, Christopher Lamey <clamey@localmatters.com> wrote:
>> 
>> I think the use of the Resource element in the server.xml is the same as
>> using the resource-ref element in the web.xml.  From the docs:
>> 
>>     <Resource> - Configure the name and data type of a resource made
>> available to the application (equivalent to the inclusion of a
>> <resource-ref> element in the web application deployment descriptor).
>> 
>> So you can probably remove that from your web.xml.
>> 
>> As for the exception you're getting, you might need to use the full JNDI
>> name in iBATIS:
>> 
>>     java:/comp/env/jdbc/AS400B
>> 
>> If I remember correctly, Tomcat automatically prepends the
>> "java:/comp/env/"
>> bit to declared JNDI resources.
>> 
>> Cheers,
>> Chris
>> 
>> On 9/27/07 9:32 AM, "Ashish Kulkarni" <ashish.kulkarni13@gmail.com> wrote:
>> 
>>> Hi
>>> This is what i have done,
>>> in my server.xml file i have added in host element
>>> <Context path="/approvals6302" docBase="c:\\approvals6302\\" debug="5"
>>> reloadable="true" crossContext="true">
>>> <Resource name="jdbc/AS400B" type="javax.sql.DataSource"
>> password="password"
>>>   driverClassName="com.ibm.as400.access.AS400JDBCDriver" maxIdle="2"
>>> maxWait="5000" validationQuery="select * from PODATA" username="userid"
>>> url="jdbc:as400:AS400B/RPTSTEXTDB;naming=system;date format=iso;time
>>> format=hms;prompt=false" maxActive="4" />
>>> </Context>
>>> 
>>> Then in web.xml i have added
>>> <resource-ref>
>>>   <description>DB Connection</description>
>>>    <res-ref-name>jdbc/AS400B</res-ref-name>
>>>    <res-type>javax.sql.DataSource</res-type>
>>>    <res-auth>Container</res-auth>
>>> </resource-ref>
>>> 
>>> and in SqlMapConfig.xml i have added
>>> <transactionManager type="JDBC">
>>>     <dataSource type="JNDI">
>>>        <property name="DataSource" value="jdbc/AS400B"/>
>>>     </dataSource>
>>> </transactionManager>
>>> 
>>> When i start tomcat i get the following error
>>> com.ibatis.sqlmap.client.SqlMapException: There was an error configuring
>>> JndiDataSourceDaoTransactionPool. Cause:
>> javax.naming.NameNotFoundException:
>>> Name jdbc is not bound in this Context
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> On 9/27/07, Christopher Lamey <clamey@localmatters.com> wrote:
>>>> 
>>>> Hello,
>>>> 
>>>> You then need to add a ResourceLink either in conf/context.xml or in
>> the
>>>> webapp's specific context file (if you're using one).
>>>> 
>>>> I don't know what version of Tomcat you're using, but here are the JNDI
>>>> docs
>>>> for 5.5:
>>>> 
>>>> http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html
>>>> 
>>>> PS: I think you can add a resource-env-ref to your webapp's web.xml to
>>>> link
>>>> to the global JNDI resource too, but I usually go with the ResourceLink
>> in
>>>> the context files to avoid mucking with web.xml.
>>>> 
>>>> Cheers,
>>>> Chris
>>>> 
>>>> On 9/27/07 7:58 AM, "Ashish Kulkarni" <ashish.kulkarni13@gmail.com>
>> wrote:
>>>> 
>>>>> Hi
>>>>> I am using tomcat application server, and using the Administator
>>>> application
>>>>> i added JNDI for my database, i found that it had added following
>> entry
>>>> in
>>>>> server.xml file
>>>>> 
>>>>> <Resource
>>>>> name="AS400B"
>>>>> type="javax.sql.DataSource"
>>>>> password="password"
>>>>> driverClassName="com.ibm.as400.access.AS400JDBCDriver"
>>>>> maxIdle="2"
>>>>> maxWait="5000"
>>>>> validationQuery="select * from PODATA"
>>>>> username="userid"
>>>>> url="jdbc:as400:AS400B/RPTSTEXTDB;naming=system;date format=iso;time
>>>>> format=hms;prompt=false"
>>>>> maxActive="4"/>
>>>>> 
>>>>> 
>>>>> On 9/26/07, Richard Yee <ryee@cruzio.com> wrote:
>>>>>> 
>>>>>> You need to set up the connection pool in your application server
>>>>>> configuration. What AppServer are you using?
>>>>>> 
>>>>>> -Richard
>>>>>> 
>>>>>> Ashish Kulkarni wrote:
>>>>>>> Hi
>>>>>>> I did modify this to look like below
>>>>>>> <transactionManager type="JDBC">
>>>>>>>     <dataSource type="JNDI">
>>>>>>>           <property name="DataSource" value="AS400B"/>
>>>>>>>     </dataSource>
>>>>>>> </transactionManager>
>>>>>>> 
>>>>>>> But i get following error in tomcat, I have created a data sourse
>>>>>>> AS400B in tomcat
>>>>>>> 
>>>>>>> Caused by: javax.naming.NameNotFoundException: Name AS400B is
not
>>>>>>> bound in this Context
>>>>>>> at
>>>>>>> com.ibatis.sqlmap.engine.builder.xml.SqlMapConfigParser.parse(
>>>>>> SqlMapConfigParser.java:81)
>>>>>>> at
>>>>>>> com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(
>>>>>> SqlMapClientBuilder.java:62)
>>>>>>> at com.pfizer.maps.data.GetSQLConfigForWeb.getSqlMapInstance
>>>>>>> (GetSQLConfigForWeb.java:49)
>>>>>>> at
>>>>>>> com.pfizer.maps.servlet.Log4JInitServlet.init(Log4JInitServlet.java
>>>> :104)
>>>>>>> at
>>>>>>> org.apache.catalina.core.StandardWrapper.loadServlet(
>>>>>> StandardWrapper.java:1105)
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> On 9/26/07, *Ashish Kulkarni* <ashish.kulkarni13@gmail.com
>>>>>>> <mailto:ashish.kulkarni13@gmail.com>> wrote:
>>>>>>> 
>>>>>>>     Hi
>>>>>>>     Is there any example of setting up ibatis to use JNDI as
data
>>>>>> source,
>>>>>>>     I have the following, how do i change it to use JNDI
>>>>>>> 
>>>>>>>     <properties resource="SqlMapConfigAS400.properties" />
>>>>>>>     <transactionManager type="JDBC">
>>>>>>>     <dataSource type="SIMPLE">
>>>>>>>     <property name="JDBC.Driver" value="${driver}" />
>>>>>>> 
>>>>>>>     <property name="JDBC.ConnectionURL" value="${url}" />
>>>>>>> 
>>>>>>>     <property name="JDBC.Username" value="${username}" />
>>>>>>> 
>>>>>>>     <property name="JDBC.Password" value="${password}" />
>>>>>>>     </dataSource>
>>>>>>>     </transactionManager>
>>>>>>> 
>>>>>>>     And i have SqlMapConfigAS400.properties where i have
>>>>>>>     driver=com.ibm.as400.access.AS400JDBCDriver
>>>>>>> 
>>>>>>>     url=jdbc:as400:AS400B/RPTSTEXTDB;naming=system;date
>>>>>>>     format=iso;time format=hms;prompt=false
>>>>>>> 
>>>>>>>     username=user
>>>>>>> 
>>>>>>>     password=password
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>> 
>>>>>> 
>>>> 
>>>> 
>> 
>> 


Mime
View raw message