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 Folashade Adeyosoye <sh...@gmail.com> on 2005/05/24 19:36:35 UTC

Sample Code using javax.sql.DataSource

Can some please post a sample code using the javax.sql.DataSource; to get
connection and to also close the connection in the try final..

 

Something like this.

 

Thanks

 

 

import javax.sql.DataSource;

 

DataSource dataSource ;

 

    try {

      connection = dataSource.getConnection();

      statement = connection.prepareStatement(sql.toString());

 

      statement.setString(1, APPROVED);

      statement.setString(2, objectType);

 

      resultSet = statement.executeQuery();

 

      objectList = copyResultSetToVO(resultSet);

    }

    catch (SQLException sqlEx) {

      throw new DaoException(sqlEx.getMessage());

    }

    catch (Exception ex) {

      throw new DaoException(ex.getMessage());

    }

    finally {

      //DBUtils.closeResultSet(resultSet);

     // DBUtils.closeStatement(statement);

      //DBUtils.closeConnection(connection);

 

            HOW DO I MAKE SURE THE CONNECTION IS PLACED BACK IN THE DAO POOL
TO BE REUSED

    }

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?>

 

<!DOCTYPE daoConfig

    PUBLIC "-//iBATIS.com//DTD DAO Configuration 2.0//EN"

    "http://www.ibatis.com/dtd/dao-2.dtd">

 

<daoConfig>

 

<properties
resource="net/naijatek/fgconline/properties/database_local.properties"/>

 

  <context>

 

   <transactionManager type="JDBC">

      <property name="DataSource" value="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}"/>

      <property name="JDBC.DefaultAutoCommit" value="true"/>

      <property name="Pool.MaximumActiveConnections" value="10"/>

      <property name="Pool.MaximumIdleConnections" value="5"/>

      <property name="Pool.MaximumCheckoutTime" value="120000"/>

    </transactionManager>

 

    <dao interface="net.naijatek.fgconline.persistence.iface.MemberDAO"
implementation="net.naijatek.fgconline.persistence.jdbcsql.MysqlMemberDAO"/>

  </context>

</daoConfig>

 


RE: Sample Code using javax.sql.DataSource

Posted by Folashade Adeyosoye <sh...@gmail.com>.
Thank you very much; I actually extended "JdbcDaoTemplate" in my
"BaseJdbcDao" class that is in turn extended in my MysqlMemberDAO

 

 

 

public class BaseJdbcDao extends JdbcDaoTemplate {

 

 

public class MysqlMemberDAO extends BaseJdbcDao implements MemberDAO {

 

 

 And now I can just use   "getConnection()" in the MysqlMemberDAO class.

 

 

Thanks

 

 

 

 

  _____  

From: Clinton Begin [mailto:clinton.begin@gmail.com] 
Sent: Wednesday, May 25, 2005 12:34 AM
To: ibatis-user-java@incubator.apache.org
Subject: Re: Sample Code using javax.sql.DataSource

 


That code is very incomplete.  It's hard to see what you're trying to do.

Why are you accessing the DataSource directly?  How are you even doing that?

In any case, why don't you just use the JdbcDaoTemplate and call
getConnection().  Then you never have to worry about opening and closing
connections (something you shouldn't be doing inside of a DAO anyway).

Clinton



On 5/24/05, Folashade Adeyosoye <sh...@gmail.com> wrote:

Can some please post a sample code using the javax.sql.DataSource; to get
connection and to also close the connection in the try final..

 

Something like this.

 

Thanks

 

 

import javax.sql.DataSource;

 

DataSource dataSource ;

 

    try {

      connection = dataSource.getConnection();

      statement = connection.prepareStatement(sql.toString());

 

      statement.setString(1, APPROVED);

      statement.setString(2, objectType);

 

      resultSet = statement.executeQuery();

 

      objectList = copyResultSetToVO(resultSet);

    }

    catch (SQLException sqlEx) {

      throw new DaoException(sqlEx.getMessage());

    }

    catch (Exception ex) {

      throw new DaoException(ex.getMessage());

    }

    finally {

      //DBUtils.closeResultSet(resultSet);

     // DBUtils.closeStatement(statement);

      //DBUtils.closeConnection(connection);

 

            HOW DO I MAKE SURE THE CONNECTION IS PLACED BACK IN THE DAO POOL
TO BE REUSED

    }

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?>

 

<!DOCTYPE daoConfig

    PUBLIC "-//iBATIS.com//DTD DAO Configuration 2.0//EN"

    "http://www.ibatis.com/dtd/dao-2.dtd
<http://www.ibatis.com/dtd/dao-2.dtd%22> ">

 

<daoConfig>

 

<properties
resource="net/naijatek/fgconline/properties/database_local.properties"/>

 

  <context>

 

   <transactionManager type="JDBC">

      <property name="DataSource" value="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}"/>

      <property name="JDBC.DefaultAutoCommit" value="true"/>

      <property name="Pool.MaximumActiveConnections" value="10"/>

      <property name="Pool.MaximumIdleConnections" value="5"/>

      <property name="Pool.MaximumCheckoutTime" value="120000"/>

    </transactionManager>

 

    <dao interface="net.naijatek.fgconline.persistence.iface.MemberDAO"
implementation="net.naijatek.fgconline.persistence.jdbcsql.MysqlMemberDAO"/>

  </context>

</daoConfig>

 

 


Re: Sample Code using javax.sql.DataSource

Posted by Clinton Begin <cl...@gmail.com>.
That code is very incomplete. It's hard to see what you're trying to do.

Why are you accessing the DataSource directly? How are you even doing that?

In any case, why don't you just use the JdbcDaoTemplate and call 
getConnection(). Then you never have to worry about opening and closing 
connections (something you shouldn't be doing inside of a DAO anyway).

Clinton


On 5/24/05, Folashade Adeyosoye <sh...@gmail.com> wrote:
> 
>  Can some please post a sample code using the javax.sql.DataSource; to get 
> connection and to also close the connection in the try final….
> 
>  Something like this…
> 
>  Thanks
> 
>   import javax.sql.DataSource;
> 
>  DataSource dataSource ;
> 
>  try {
> 
> connection = dataSource.getConnection();
> 
> statement = connection.prepareStatement(sql.toString());
> 
>  statement.setString(1, APPROVED);
> 
> statement.setString(2, objectType);
> 
>  resultSet = statement.executeQuery();
> 
>  objectList = copyResultSetToVO(resultSet);
> 
> }
> 
> catch (SQLException sqlEx) {
> 
> throw new DaoException(sqlEx.getMessage());
> 
> }
> 
> catch (Exception ex) {
> 
> throw new DaoException(ex.getMessage());
> 
> }
> 
> finally {
> 
> //DBUtils.closeResultSet(resultSet);
> 
> // DBUtils.closeStatement(statement);
> 
> //DBUtils.closeConnection(connection);
> 
>  *HOW DO I MAKE SURE THE CONNECTION IS PLACED BACK IN THE DAO POOL TO BE 
> REUSED*
> 
> }
> 
>      <?xml version="1.0" encoding="UTF-8"?>
> 
>  <!DOCTYPE daoConfig
> 
> PUBLIC "-//iBATIS.com//DTD DAO Configuration 2.0//EN"
> 
> "http://www.ibatis.com/dtd/dao-2.dtd"<http://www.ibatis.com/dtd/dao-2.dtd%22>
> >
> 
>  <daoConfig>
> 
>  <properties 
> resource="net/naijatek/fgconline/properties/database_local.properties"/>
> 
>  <context>
> 
>  <transactionManager type="JDBC">
> 
> <property name="DataSource" value="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}"/>
> 
> <property name="JDBC.DefaultAutoCommit" value="true"/>
> 
> <property name="Pool.MaximumActiveConnections" value="10"/>
> 
> <property name="Pool.MaximumIdleConnections" value="5"/>
> 
> <property name="Pool.MaximumCheckoutTime" value="120000"/>
> 
> </transactionManager>
> 
>  <dao interface="net.naijatek.fgconline.persistence.iface.MemberDAO" 
> implementation="net.naijatek.fgconline.persistence.jdbcsql.MysqlMemberDAO
> "/>
> 
> </context>
>  
> </daoConfig>
>  
>