ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Brandon Goodin <brandon.goo...@gmail.com>
Subject Re: Access to the underlying connection
Date Tue, 19 Apr 2005 15:55:39 GMT
I dug a little deeper. Forgot about this...

  public JdbcDaoTransaction(DataSource dataSource) {
    try {
      connection = dataSource.getConnection();
      if (connection == null) {
        throw new DaoException("Could not start transaction.  Cause:
The DataSource returned a null connection.");
      }
      if (connection.getAutoCommit()) {
        connection.setAutoCommit(false);
      }
      if (connectionLog.isDebugEnabled()) {
        connection = ConnectionLogProxy.newInstance(connection);
      }
    } catch (SQLException e) {
      throw new DaoException("Error starting JDBC transaction.  Cause: " + e);
    }
  }

You can see that when logging at a debugging level is enabled you get
a proxy. This would interfere with casting to an oracle connection for
sure.

if (connectionLog.isDebugEnabled()) {
        connection = ConnectionLogProxy.newInstance(connection);
      }

Otherwise you should get a straight connection.

Brandon

On 4/19/05, Brandon Goodin <brandon.goodin@gmail.com> wrote:
> It's not a proxy. Here are the steps that the DAO Framework takes to
> setup JNDI with JdbcTransactionManager.
> 
> During the parsing of the Dao configuration file the configure method
> is called for the transaction manager:
> 
> public class JdbcDaoTransactionManager implements DaoTransactionManager {
> 
> ...
> 
>   public void configure(Properties properties) {
>     if (properties.containsKey("DataSource")) {
>       String type = (String) properties.get("DataSource");
>       if ("SIMPLE".equals(type)) {
>         configureSimpleDataSource(properties);
>       } else if ("DBCP".equals(type)) {
>         configureDbcp(properties);
>       } else if ("JNDI".equals(type)) {
>         configureJndi(properties);
>       } else {
>         throw new DaoException("DAO Transaction Manager properties
> must include a value for 'DataSource' of SIMPLE, DBCP or JNDI.");
>       }
>     } else {
>       throw new DaoException("DAO Transaction Manager properties must
> include a value for 'DataSource' of SIMPLE, DBCP or JNDI.");
>     }
>   }
> ...
> 
> which calls the configureJndi(properties) method and sets the datasource.
> 
> ...
>   private void configureJndi(Map properties) {
>     try {
>       Hashtable contextProps = getContextProperties(properties);
>       InitialContext initCtx = null;
>       if (contextProps == null) {
>         initCtx = new InitialContext();
>       } else {
>         initCtx = new InitialContext(contextProps);
>       }
>       dataSource = (DataSource) initCtx.lookup((String)
> properties.get("DBJndiContext"));
>     } catch (NamingException e) {
>       throw new DaoException("There was an error configuring the
> DataSource from JNDI.  Cause: " + e, e);
>     }
>   }
> ...
> 
> As you can see it is a straight datasource that is created... no proxies.
> 
> Your Dao class would extend the JdbcDaoTemplate which would provide
> you access to the connection for the current transaction. Again you
> can see that there are no proxies.
> 
> public abstract class JdbcDaoTemplate extends DaoTemplate {
> ...
>   /**
>    * Gets the JDBC Connection associated with the current
>    * DaoTransaction that this Dao is working under.
>    *
>    * @return A JDBC Connection instance.
>    */
>   protected Connection getConnection() {
>     DaoTransaction trans = daoManager.getTransaction(this);
>     if (!(trans instanceof ConnectionDaoTransaction)) {
>       throw new DaoException("The DAO manager of type " +
> daoManager.getClass().getName() +
>           " cannot supply a JDBC Connection for this template, and is
> therefore not" +
>           "supported by JdbcDaoTemplate.");
>     }
>     return ((ConnectionDaoTransaction) trans).getConnection();
>   }
> ...
> }
> 
> I hope that helps,
> Brandon
> 
> On 4/19/05, Richard Osbaldeston <richard.osbaldeston@wordmap.com> wrote:
> > Its a bit late for that, I've just butchered the code removing all
> > traces of IBatis.. Out of interest whats the class of the returned
> > connection, conn? I notice you don't close it so its likely to be
> > another proxy - in which case it dosnt help as I cant create a oracle
> > array descriptor using it (classcastexception).
> >
> > - Richard
> >
> > Brandon Goodin wrote:
> >
> > >Richard,
> > >
> > >If you configure your oracle datasrouce via JNDI you should have no
> > >problem accomplishing what you want.
> > >
> > >Example:
> > >
> > >public class JdbcAccountDao extends JdbcDaoTemplate implements AccountDao {
> > >...
> > >  public JdbcAccountDao(DaoManager daoManager) {
> > >    super(daoManager);
> > >  }
> > >...
> > >
> > >  public Account findAccount(int id) {
> > >    Account account = null;
> > >    Connection conn = getConnection();
> > >    PreparedStatement ps = null;
> > >    ResultSet rs = null;
> > >    try {
> > >      ps = conn.prepareStatement(SELECT);
> > >      ps.setInt(1, id);
> > >      rs = ps.executeQuery();
> > >      while (rs.next()) {
> > >        account = new Account();
> > >        account.setId(rs.getInt("id"));
> > >        account.setFirstName(rs.getString("firstName"));
> > >        account.setLastName(rs.getString("lastName"));
> > >        account.setEmailAddress(rs.getString("emailAddress"));
> > >      }
> > >    } catch (SQLException e) {
> > >      throw new DaoException("Error finding Account.  Cause: " + e, e);
> > >    } finally {  
> > >      closeResultSet(rs);
> > >      closePreparedStatement(ps);
> > >    }
> > >    return account;
> > >  }
> > >...
> > >}
> > >
> > >
> > >Brandon
> > >
> > >On 4/19/05, Richard Osbaldeston <richard.osbaldeston@wordmap.com> wrote:
> > >
> > >
> > >>Brandon Goodin wrote:
> > >>
> > >>
> > >>
> > >>>I thought Oracle has a pooled connection datasource. Would it be
> > >>>possible to use oracle's datasource via JNDI? I would think that their
> > >>>implementation would provide what you need.
> > >>>
> > >>>Brandon
> > >>>
> > >>>
> > >>>
> > >>>
> > >>Hmmm, only if I could still get hold of the 'real' connection and not
> > >>the proxy that IBatis returns (I guess so the IBatis developer dosnt
> > >>have to worry about closing it correctly). But the fact that they'res
> > >>more than one way to configure the datasources strongly suggests I'm
> > >>barking up the wrong tree with the bugs in SimpleDataSource.. if the
> > >>user configures a different datasource the same problem will re-occur.
> > >>Guess there's just too much Oracle in this equation.. I'll have to drop
> > >>IBatis from this project. Shame (and a lot of back-pedalling on my account).
> > >>
> > >>- Richard
> > >>
> > >>
> > >>
> > >
> > >
> > >
> >
> >
>

Mime
View raw message