ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Bharat Nagwani" <bnagw...@juniper.net>
Subject RE: Database Connection without Datasource
Date Tue, 01 Mar 2005 19:32:28 GMT
Thanks much. This will work.

-----Original Message-----
From: Brandon Goodin [mailto:brandon.goodin@gmail.com] 
Sent: Tuesday, March 01, 2005 11:22 AM
To: ibatis-user-java@incubator.apache.org
Subject: Re: Database Connection without Datasource

You can pass a connection into ibatis. Look into the
SqlMapClient.openSession(Connection conn); But, you will have to
manage the connection opening and closing yourself.

Following is the javadoc for this:

openSession
public SqlMapSession openSession(java.sql.Connection conn)

Returns a single threaded SqlMapSession implementation for use by one
user. Remember though, that SqlMapClient itself is a thread safe
SqlMapSession implementation, so you can also just work directly with
it. If you do get a session explicitly using this method be sure to
close it! You can close a session using the SqlMapSession.close()
method.
This particular implementation takes a user provided connection as a
parameter. This connection will be used for executing statements, and
therefore overrides any configured datasources. Using this approach
allows the developer to easily use an externally supplied connection
for executing statements.

Important: Using a user supplied connection basically sidesteps the
datasource so you are responsible for appropriately handling your
connection lifecycle (i.e. closing). Here's a (very) simple example
(throws SQLException):

 try {
   Connection connection = dataSource.getConnection();
   SqlMapSession session = sqlMap.openSession(connection);
   // do work
   connection.commit();
 } catch (SQLException e) {
     try {
       if (connection != null) commit.rollback();
     } catch (SQLException ignored) {
       // generally ignored
     }
     throw e;  // rethrow the exception
 } finally {
   try {
     if (connection != null) connection.close();
   } catch (SQLException ignored) {
     // generally ignored
   }
 }


Brandon


On Tue, 1 Mar 2005 10:56:56 -0800, Bharat Nagwani <bnagwani@juniper.net>
wrote:
> Thanks for the replies.
> I think it is just little expensive to create a datasource object and
a
> Conn. Pool with 1 connection, When all my code needs is to get a
single
> connection do some processing and quit. It also means I am dependent
on
> some
> framework like dbcp for DataSource implementation.
> 
> thanks
> 
> -----Original Message-----
> From: Kris Jenkins [mailto:krisajenkins@yahoo.co.uk]
> Sent: Tuesday, March 01, 2005 10:39 AM
> To: ibatis-user-java@incubator.apache.org
> Subject: Re: Database Connection without Datasource
> 
> I'm pretty sure dataSource is mandatory (after all, you've got to have
> some kind of source for your data!).  Whenever I've wanted to do what
> you describe, I've just used <dataSource type="SIMPLE"> and set
> <property name="Pool.MaximumActiveConnections" value="1"/>
> 
> HTH,
> Kris
> 
> >
> >
> > Hi,
> >
> >
> >
> > Is there a way to specify connection properties in xml file to
create
> > a single connection (no conn. Pool) instead of using datasource?
Lets
> say
> >
> > my application runs once per day. It creates a connection does some
> > processing and ends. Datasource
> >
> > in this case is not a right choice.
> >
> >
> >
> > thanks
> >
> 
> --
> Kris Jenkins
> Email:  kris@jenkster.com
> Blog:   http://cafe.jenkster.com/
> Wiki:   http://wiki.jenkster.com/
> 
>

Mime
View raw message