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 Adinath <ad...@acciente.com> on 2010/04/18 22:31:21 UTC
Transaction management in iBATIS 3 inside an EJB (3.1) container
Is anybody out there using iBATIS 3 as their persistence framework inside an
EJB container? I recently started building a new system for which I choose
EJB 3.1 (the version of EJB is actually irrelevant to this question) as my
app framework and iBATIS 3 (this version is relevant!) as my persistence
framework. My business logic is implemented in EJB 3.1 session beans that
use iBATIS 3 for data access. I am running on GlassFish v3)
My issue with this stack has been transaction management. I have solved my
problem by writing some simple integration code, but I was little surprised
I had to do this. So I decided to post this to see if others have run into
this and if so how they solved the problem.
My requirement is for iBATIS 3 to transparently use the EJB transaction
(typically defined declaratively) in the session bean method. iBATIS 3
provides 2 transaction factories JdbcTransactionFactory and
ManagedTransactionFactory and I found that neither one works correctly in an
EJB environment (and looking at the iBATIS source it is clear why it fails).
The JdbcTransactionFactory is inappropriate since I want any calls to
sqlSession.commit() or sqlSession.rollback() to be ignored. So I thought ok
I should use the ManagedTransactionFactory since it does cause any calls to
sqlSession.commit() or sqlSession.rollback() to be ignored, however it also
causes sqlSession.close() to *not* close the connection that iBATIS opened
from the DataSource in sqlSession.open() (the DataSource is a container
managed DataSource object I provide to iBATIS). This causes the GlassFish to
exhaust its connect pool and the application fails.
So I wrote a new TransactionFactory implementation, EJBTransactionFactory,
that causes sqlSession.commit() or sqlSession.rollback() to do nothing, but
*does close* the connection when sqlSession.close() is called.
I suspect other people have run into this, how did you solve it?
Thanks,
Adi
--
Acciente, LLC
Systems Architecture and Software Design
www.acciente.com
www.inductionframework.org
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Clinton Begin <cl...@gmail.com>.
Thanks for the Websphere background. Indeed it seems that this was left as
an implementation detail. But Glassfish and Websphere are a pretty good
cross section of how it should work.
Cheers,
Clinton
On Thu, Apr 22, 2010 at 3:32 PM, Adinath Raveendra Raj <adinath@acciente.com
> wrote:
> Clinton,
>
> On Tue, Apr 20, 2010 at 7:48 PM, Clinton Begin <cl...@gmail.com>wrote:
>
>> Okay, I've looked around. It is really hard to find the way it's
>> "supposed" to work, but I don't think it matters. There's more than enough
>> empirical evidence (both in iBATIS 2 and Adinath's custom implementation).
>> So I've added the close to the ManagedTransaction. It will log but
>> otherwise ignore exceptions thrown by close(), to deal with unexpected
>> states (and re-throwing an exception from close could hide a more important
>> root cause exception).
>>
>
> It is hard to find info on this, there is a WebSphere (from 2005) article (
> http://www.ibm.com/developerworks/websphere/library/techarticles/0506_johnsen/0506_johnsen.html)
> about this topic that says:
>
> *When the application closes a shareable connection, the connection is not
> truly closed, nor is it returned to the Free pool. Rather, it remains in the
> Shared connection pool, ready for another request within the same LTC for a
> connection to the same resource.
>
> ** LTC = Local Transaction Containment
>
> The funny thing is that I even had a unit test asserting this behavior, so
>> I must have done it at some point for a reason. I just hope it wasn't a very
>> good reason. ;-)
>
>
> Haha, been there :)
>
>
>> It's also funny that it wasn't found in 10 betas... which either implies
>> that not many people use managed transactions, or that some app servers
>> behave as I thought they would.
>>
>> I'm going to recommend a re-build before we take iBATIS 3 to GA, so the
>> iBATIS release we'll vote on will probably be 3.0.1.
>>
>
> Thanks, I think your changes are going to help others avoid this issue.
> Keep up the excellent work.
>
> Best,
> Adi
>
> --
> Acciente, LLC
> Systems Architecture and Software Design
>
> www.acciente.com
> www.inductionframework.org
>
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Adinath Raveendra Raj <ad...@acciente.com>.
Clinton,
On Tue, Apr 20, 2010 at 7:48 PM, Clinton Begin <cl...@gmail.com>wrote:
> Okay, I've looked around. It is really hard to find the way it's
> "supposed" to work, but I don't think it matters. There's more than enough
> empirical evidence (both in iBATIS 2 and Adinath's custom implementation).
> So I've added the close to the ManagedTransaction. It will log but
> otherwise ignore exceptions thrown by close(), to deal with unexpected
> states (and re-throwing an exception from close could hide a more important
> root cause exception).
>
It is hard to find info on this, there is a WebSphere (from 2005) article (
http://www.ibm.com/developerworks/websphere/library/techarticles/0506_johnsen/0506_johnsen.html)
about this topic that says:
*When the application closes a shareable connection, the connection is not
truly closed, nor is it returned to the Free pool. Rather, it remains in the
Shared connection pool, ready for another request within the same LTC for a
connection to the same resource.
** LTC = Local Transaction Containment
The funny thing is that I even had a unit test asserting this behavior, so I
> must have done it at some point for a reason. I just hope it wasn't a very
> good reason. ;-)
Haha, been there :)
> It's also funny that it wasn't found in 10 betas... which either implies
> that not many people use managed transactions, or that some app servers
> behave as I thought they would.
>
> I'm going to recommend a re-build before we take iBATIS 3 to GA, so the
> iBATIS release we'll vote on will probably be 3.0.1.
>
Thanks, I think your changes are going to help others avoid this issue. Keep
up the excellent work.
Best,
Adi
--
Acciente, LLC
Systems Architecture and Software Design
www.acciente.com
www.inductionframework.org
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Clinton Begin <cl...@gmail.com>.
Okay, I've looked around. It is really hard to find the way it's "supposed"
to work, but I don't think it matters. There's more than enough empirical
evidence (both in iBATIS 2 and Adinath's custom implementation). So I've
added the close to the ManagedTransaction. It will log but otherwise ignore
exceptions thrown by close(), to deal with unexpected states (and
re-throwing an exception from close could hide a more important root cause
exception).
The funny thing is that I even had a unit test asserting this behavior, so I
must have done it at some point for a reason. I just hope it wasn't a very
good reason. ;-) It's also funny that it wasn't found in 10 betas...
which either implies that not many people use managed transactions, or that
some app servers behave as I thought they would.
I'm going to recommend a re-build before we take iBATIS 3 to GA, so the
iBATIS release we'll vote on will probably be 3.0.1.
Cheers,
Clinton
On Mon, Apr 19, 2010 at 7:54 PM, Guy Rouillier <gu...@burntmail.com>wrote:
> On 4/19/2010 2:07 PM, Adinath wrote:
>
>>
>> Regarding your thoughts about a smart resource manager, that is
>> indeed what happens. Remember that datasource may be retrieved and
>> closed any number of times during the processing of a single EJB
>> method. During the execution of single EJB method, the container is
>> required by the spec to return the same physical connection each
>> time getConnection() is invoked, so that all database activity
>> within that method (which can of course call other methods) is
>> guaranteed to be within the same transaction.
>>
>>
>> What about a method in bean A that call a method a different bean say B,
>> does the spec require that B gets the same underlying connection? In
>> GlassFish I suspect I am getting completely independent connections, I
>> need to verify this.
>>
>
> Should return the same connection. One EJB is allowed to invoke another,
> and the container is supposed to identify that it is already in a
> transaction and not start a new one.
>
>
> Since iBATIS can't possible determine that it is being used in a
>> container-managed transaction, if you want to support that, I would
>> think the only possibility is to provide an optional parameter in
>> the data source configuration to indicate that. In such cases,
>> iBATIS should probably no-op commit and rollback.
>>
>>
>> IBATIS does not need a config parameter, you can do this by implementing
>> a simple transaction factory.
>>
>
> I'm not following you. iBATIS is told to use a JNDI datasource X. How can
> it determine if it is running (e.g.) in a JBoss environment with a
> no-tx-datasource, in which case it needs to do commit() and rollback()
> before closing the connection? Or if it is running in a JBoss environment
> with a local-tx-datasource, in which case it cannot do a commit() or
> rollback() before closing the connection?
>
> Are you suggesting that in the second case, the programmer can supply a
> do-nothing transaction manager? That approach imposes that requirement on
> everyone who uses iBATIS. Certainly just supplying a param makes life
> easier for a lot of people.
>
>
> --
> Guy Rouillier
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
> For additional commands, e-mail: user-java-help@ibatis.apache.org
>
>
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Guy Rouillier <gu...@burntmail.com>.
On 4/19/2010 2:07 PM, Adinath wrote:
>
> Regarding your thoughts about a smart resource manager, that is
> indeed what happens. Remember that datasource may be retrieved and
> closed any number of times during the processing of a single EJB
> method. During the execution of single EJB method, the container is
> required by the spec to return the same physical connection each
> time getConnection() is invoked, so that all database activity
> within that method (which can of course call other methods) is
> guaranteed to be within the same transaction.
>
>
> What about a method in bean A that call a method a different bean say B,
> does the spec require that B gets the same underlying connection? In
> GlassFish I suspect I am getting completely independent connections, I
> need to verify this.
Should return the same connection. One EJB is allowed to invoke
another, and the container is supposed to identify that it is already in
a transaction and not start a new one.
> Since iBATIS can't possible determine that it is being used in a
> container-managed transaction, if you want to support that, I would
> think the only possibility is to provide an optional parameter in
> the data source configuration to indicate that. In such cases,
> iBATIS should probably no-op commit and rollback.
>
>
> IBATIS does not need a config parameter, you can do this by implementing
> a simple transaction factory.
I'm not following you. iBATIS is told to use a JNDI datasource X. How
can it determine if it is running (e.g.) in a JBoss environment with a
no-tx-datasource, in which case it needs to do commit() and rollback()
before closing the connection? Or if it is running in a JBoss
environment with a local-tx-datasource, in which case it cannot do a
commit() or rollback() before closing the connection?
Are you suggesting that in the second case, the programmer can supply a
do-nothing transaction manager? That approach imposes that requirement
on everyone who uses iBATIS. Certainly just supplying a param makes
life easier for a lot of people.
--
Guy Rouillier
---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Adinath <ad...@acciente.com>.
On Mon, Apr 19, 2010 at 10:33 AM, Guy Rouillier <gu...@burntmail.com>wrote:
> I haven't been following this thread closely, but with JBoss at least, you
> have the option of configuring datasources as no-transaction:
> no-tx-datasource. With this type of datasource, you are free to call commit
> and rollback yourself. With container-managed transactions, you should not
> be calling commit or rollback. I've erroneously done so, and JBoss
> generates an error saying that these methods are illegal on a container
> managed transaction.
>
I agree, commit()/rollback() should not be called on the container managed
connections.
>
> Regarding your thoughts about a smart resource manager, that is indeed what
> happens. Remember that datasource may be retrieved and closed any number of
> times during the processing of a single EJB method. During the execution of
> single EJB method, the container is required by the spec to return the same
> physical connection each time getConnection() is invoked, so that all
> database activity within that method (which can of course call other
> methods) is guaranteed to be within the same transaction.
>
What about a method in bean A that call a method a different bean say B,
does the spec require that B gets the same underlying connection? In
GlassFish I suspect I am getting completely independent connections, I need
to verify this.
> Only when the EJB method completes does the container do the commit or
> rollback and then returns the connection to the pool as available for use.
>
> Since iBATIS can't possible determine that it is being used in a
> container-managed transaction, if you want to support that, I would think
> the only possibility is to provide an optional parameter in the data source
> configuration to indicate that. In such cases, iBATIS should probably no-op
> commit and rollback.
IBATIS does not need a config parameter, you can do this by implementing a
simple transaction factory.
Best,
Adi
--
Acciente, LLC
Systems Architecture and Software Design
www.acciente.com
www.inductionframework.org
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Guy Rouillier <gu...@burntmail.com>.
I haven't been following this thread closely, but with JBoss at least,
you have the option of configuring datasources as no-transaction:
no-tx-datasource. With this type of datasource, you are free to call
commit and rollback yourself. With container-managed transactions, you
should not be calling commit or rollback. I've erroneously done so, and
JBoss generates an error saying that these methods are illegal on a
container managed transaction.
Regarding your thoughts about a smart resource manager, that is indeed
what happens. Remember that datasource may be retrieved and closed any
number of times during the processing of a single EJB method. During
the execution of single EJB method, the container is required by the
spec to return the same physical connection each time getConnection() is
invoked, so that all database activity within that method (which can of
course call other methods) is guaranteed to be within the same transaction.
Only when the EJB method completes does the container do the commit or
rollback and then returns the connection to the pool as available for use.
Since iBATIS can't possible determine that it is being used in a
container-managed transaction, if you want to support that, I would
think the only possibility is to provide an optional parameter in the
data source configuration to indicate that. In such cases, iBATIS
should probably no-op commit and rollback.
On 4/19/2010 11:15 AM, Clinton Begin wrote:
> Thanks Adinath.
>
> This will all help. Let's figure this out together.
>
> The odd thing for me is this. A typical transaction lifecycle (be it
> global or otherwise would be:
>
> * Open Connection
> * Do Work
> * Commit or Rollback
> * Close Connection (even if this implies a return to the connection pool)
>
> The problem with calling close on a connection that's globally managed
> is that it changes the lifecycle to:
>
> * Open Connection
> * Do Work
> * >> Close Connection (even if this implies a return to the connection pool)
> * Commit or Rollback
>
> Now I suppose a smart resource manager controlling the transaction would
> know better than to actually return the connection to the pool until the
> worker thread is finished and the transaction is either committed or
> rolled back, but I'm not entirely confident that all app servers are
> that smart. As you noted, glassfish doesn't even warn you when you call
> commit in a managed transaction (most resource managers would throw an
> exception if commit or rollback were called). I wouldn't be surprised
> if this is the reason you're getting intermittent IllegalStateExceptions.
>
> I'm going to do a little more reading tonight on JEE 6 standard behavior
> to see if there's one approach we can trust.
>
> The most empirical evidence of the proper behavior would be iBATIS 2,
> which calls close. But I normally don't remove code without reason
> (although it has happened ;-).
>
> I'll check into this tonight or tomorrow at the latest.
>
> Cheers,
> Clinton
>
>
> On Mon, Apr 19, 2010 at 1:26 AM, Adinath <adinath@acciente.com
> <ma...@acciente.com>> wrote:
>
> Clinton,
>
> On Sun, Apr 18, 2010 at 2:45 PM, Clinton Begin
> <clinton.begin@gmail.com <ma...@gmailcom>> wrote:
>
> You did the right thing... iBATIS transaction managers are meant
> to be easy to implement yourself.
>
>
> Yes, they are easy to implement!
>
> That said, this was a matter of constant debate back in the good
> old days when none of the standards were implemented
> consistently. I'm still not sure if they are.
>
> Some app servers would complain if iBATIS closed the connection,
> because if some downstream process needed a connection,
> theoretically it was supposed to use the same one (which would
> be closed by that time). I think the trick is to ensure that
> you're using a container managed DataSource. Are you retrieving
> a DataSource configured by GlassFish from a JNDI context (in
> other words, a container manged DataSource)? Or are you just
> using the iBATIS built-in SimpleDataSource?
>
>
> Yes I am using using a container managed datasource (GlassFish v3
> managing a DB2 v9.7 connection), looked up via JNDI. I create the
> iBATIS session factory using SqlSessionFactoryBuilder and the
> related classes (i.e. no XML config file).
> I
>
> I would expect a container managed DataSource to lazily
> initialize a connection for any one worker thread, and continue
> to provide that same connection to any requester for that
> particular DataSource. Then I'd expect it to close that
> connection at the end of the entire workflow.
>
>
> My experience with GlassFish is that the connection needs to be
> closed to be returned to the pool.
>
> With GlassFish I wrote a set of tests and established that if you
> call commit() on a JDBC connection returned by the container managed
> datasource it causes an commit (thru to the driver), defeating the
> EJB transaction.
>
> There are 2 other issues I ommitted in my original post, for
> clarity. I suspect these 2 issues have nothing to do with IBATIS,
> but I am just sharing FYI (just in case).
>
> *Issue #1*
> In the close() method of my custom iBATIS transaction class I would
> transiently (about every 4 times) get IllegalStateException with
> "state.isBusy() : false" in the message when I call
> connection.close(). Some posts pointed that this happens when the
> connection is already closed. But I have verified using the debugger
> that this does not apply, the connection is open and isClosed()
> returns false. The fix for now is to catch the IllegalStateException
> the close() and ignore it! :(
>
> *Issue #2*
> The other issue is rather unnerving, I am reluctant to share it
> since it is a little winded, but thought perhaps it may help someone
> else. This too I suspect has nothing to do with iBATIS. It is as
> follows:
>
> - there is a stateful session bean (sfsb) A that has a reference to
> another sfsb B
> - there is a method in sfsb A, say methodOfA, which inserts a row
> using iBATIS
> - there is a method in sfsb B, say methodOfB, which inserts a row
> using direct JDBC
> - methodOfB() also uses a container managed DataSource to
> retrieve a connection and does connection.close() before returning
> - now the logic in methodOfA() looks like below:
>
> public void methodOfA()
> {
> session = sessionFactory.openSession()
>
> boolean commit = false;
> try
> {
> mapper = session.getMapper( UserMapper.class);
>
> someID = beanB.methodOfB();
>
> // set value for user
> user = new User();
> user.setOtherStuff() ...
> user.setSomeID( someID );
>
> // insert row for new user
> mapper.insertUser( user );
>
> commit = true;
> }
> finally
> {
> sessionclose();
>
> if ( ! commit )
> {
> sessionCtx.setRollbackOnly();
> }
> }
> }
>
> The issue here is that if I set a debug breakpoint at the line with
> mapper.insertUser( user ),I sometimes see the update by
> referenceToB.methodInB() get commited to the database!! (checked via
> a DB2 SQL console) This happened late Friday and I have not had a
> chance to look at since. I think the culprit here is GlassFish, I
> suspect that this happens only during debugging (need to verify this).
>
> On a final note, I have used iBATIS 2 on a previous project and
> iBATIS 3 on the current one. Even with IBATIS 2 I thought it was an
> incredible piece of work, well-thought out and a pleasure to use.
> Especially love the support for dynamic SQL, and the type handler
> callbacks. Excellent work!
>
> Best,
>
> Adi
>
> --
> Acciente, LLC
> Systems Architecture and Software Design
>
> www.acciente.com <http://www.acciente.com>
> www.inductionframework.org <http://www.inductionframework.org>
>
>
--
Guy Rouillier
---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Adinath <ad...@acciente.com>.
On Mon, Apr 19, 2010 at 8:15 AM, Clinton Begin <cl...@gmail.com>wrote:
> Thanks Adinath.
>
> This will all help. Let's figure this out together.
>
> The odd thing for me is this. A typical transaction lifecycle (be it
> global or otherwise would be:
>
> * Open Connection
> * Do Work
> * Commit or Rollback
> * Close Connection (even if this implies a return to the connection pool)
>
> The problem with calling close on a connection that's globally managed is
> that it changes the lifecycle to:
>
> * Open Connection
> * Do Work
> * >> Close Connection (even if this implies a return to the connection
> pool)
> * Commit or Rollback
>
> Now I suppose a smart resource manager controlling the transaction would
> know better than to actually return the connection to the pool until the
> worker thread is finished and the transaction is either committed or rolled
> back, but I'm not entirely confident that all app servers are that smart.
This was my exact dilemma, then I saw an article (which I cannot find now)
that suggested in a managed environment connection.close() marks the
connection for closing but does not actually return the connection to the
pool until the transaction completes, at which point commit() or rollback()
is called on the connection and the connection returned to the pool. This
made perfect sense.
I wrote a set of test cases that opened 2 JDBC connections from the managed
datasource in a session bean and performed a row insert on each connection
to a test table (same table for both inserts). The observation were
consistent with the claim in the article. I observed the following (in the
context of GlassFish v3):
- the container commits the transaction (on both connections) on completion
of the session bean (i.e. no connection.commit() needed)
- committing the transaction on a connection in the code is "bad" (this was
what I expected), since it overrides the EJB transaction
- if setRollbackOnly() is called it rolls back updates made using both
connections
- my attempts to look inside the wrapped connection objects returned show no
evidence that I am getting the same underlying connection handle
> As you noted, glassfish doesn't even warn you when you call commit in a
> managed transaction (most resource managers would throw an exception if
> commit or rollback were called). I wouldn't be surprised if this is the
> reason you're getting intermittent IllegalStateExceptions.
>
Yes the lack of an exception from GlassFish is a bummer, I am going to take
a closer look at the GlassFish connection pool settings to see if they have
a bearing on this behavior.
I'm going to do a little more reading tonight on JEE 6 standard behavior to
> see if there's one approach we can trust.
>
> The most empirical evidence of the proper behavior would be iBATIS 2, which
> calls close. But I normally don't remove code without reason (although it
> has happened ;-).
>
> I'll check into this tonight or tomorrow at the latest.
>
Thanks, it seems like I am able get everything working nicely with reliable
transactions except in debug mode (incidentally I use IntelliJ IDEA Ultimate
95.54 EAP), but that is still unnerving. Will continue to look at this today
and update this thread if needed. Appreciate the excellent feedback.
Best,
Adi
--
Acciente, LLC
Systems Architecture and Software Design
www.acciente.com
www.inductionframework.org
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Clinton Begin <cl...@gmail.com>.
Thanks Adinath.
This will all help. Let's figure this out together.
The odd thing for me is this. A typical transaction lifecycle (be it global
or otherwise would be:
* Open Connection
* Do Work
* Commit or Rollback
* Close Connection (even if this implies a return to the connection pool)
The problem with calling close on a connection that's globally managed is
that it changes the lifecycle to:
* Open Connection
* Do Work
* >> Close Connection (even if this implies a return to the connection
pool)
* Commit or Rollback
Now I suppose a smart resource manager controlling the transaction would
know better than to actually return the connection to the pool until the
worker thread is finished and the transaction is either committed or rolled
back, but I'm not entirely confident that all app servers are that smart.
As you noted, glassfish doesn't even warn you when you call commit in a
managed transaction (most resource managers would throw an exception if
commit or rollback were called). I wouldn't be surprised if this is the
reason you're getting intermittent IllegalStateExceptions.
I'm going to do a little more reading tonight on JEE 6 standard behavior to
see if there's one approach we can trust.
The most empirical evidence of the proper behavior would be iBATIS 2, which
calls close. But I normally don't remove code without reason (although it
has happened ;-).
I'll check into this tonight or tomorrow at the latest.
Cheers,
Clinton
On Mon, Apr 19, 2010 at 1:26 AM, Adinath <ad...@acciente.com> wrote:
> Clinton,
>
> On Sun, Apr 18, 2010 at 2:45 PM, Clinton Begin <cl...@gmail.com>wrote:
>
>> You did the right thing... iBATIS transaction managers are meant to be
>> easy to implement yourself.
>>
>
> Yes, they are easy to implement!
>
> That said, this was a matter of constant debate back in the good old days
>> when none of the standards were implemented consistently. I'm still not
>> sure if they are.
>>
>> Some app servers would complain if iBATIS closed the connection, because
>> if some downstream process needed a connection, theoretically it was
>> supposed to use the same one (which would be closed by that time). I think
>> the trick is to ensure that you're using a container managed DataSource.
>> Are you retrieving a DataSource configured by GlassFish from a JNDI context
>> (in other words, a container manged DataSource)? Or are you just using the
>> iBATIS built-in SimpleDataSource?
>>
>
> Yes I am using using a container managed datasource (GlassFish v3 managing
> a DB2 v9.7 connection), looked up via JNDI. I create the iBATIS session
> factory using SqlSessionFactoryBuilder and the related classes (i.e. no
> XML config file).
> I
>
>> I would expect a container managed DataSource to lazily initialize a
>> connection for any one worker thread, and continue to provide that same
>> connection to any requester for that particular DataSource. Then I'd expect
>> it to close that connection at the end of the entire workflow.
>>
>
> My experience with GlassFish is that the connection needs to be closed to
> be returned to the pool.
>
> With GlassFish I wrote a set of tests and established that if you call
> commit() on a JDBC connection returned by the container managed datasource
> it causes an commit (thru to the driver), defeating the EJB transaction.
>
> There are 2 other issues I ommitted in my original post, for clarity. I
> suspect these 2 issues have nothing to do with IBATIS, but I am just sharing
> FYI (just in case).
>
> *Issue #1*
> In the close() method of my custom iBATIS transaction class I would
> transiently (about every 4 times) get IllegalStateException with
> "state.isBusy() : false" in the message when I call connection.close(). Some
> posts pointed that this happens when the connection is already closed. But I
> have verified using the debugger that this does not apply, the connection is
> open and isClosed() returns false. The fix for now is to catch the
> IllegalStateException the close() and ignore it! :(
>
> *Issue #2*
> The other issue is rather unnerving, I am reluctant to share it since it is
> a little winded, but thought perhaps it may help someone else. This too I
> suspect has nothing to do with iBATIS. It is as follows:
>
> - there is a stateful session bean (sfsb) A that has a reference to another
> sfsb B
> - there is a method in sfsb A, say methodOfA, which inserts a row using
> iBATIS
> - there is a method in sfsb B, say methodOfB, which inserts a row using
> direct JDBC
> - methodOfB() also uses a container managed DataSource to retrieve a
> connection and does connection.close() before returning
> - now the logic in methodOfA() looks like below:
>
> public void methodOfA()
> {
> session = sessionFactory.openSession()
>
> boolean commit = false;
> try
> {
> mapper = session.getMapper( UserMapper.class);
>
> someID = beanB.methodOfB();
>
> // set value for user
> user = new User();
> user.setOtherStuff() ...
> user.setSomeID( someID );
>
> // insert row for new user
> mapper.insertUser( user );
>
> commit = true;
> }
> finally
> {
> session.close();
>
> if ( ! commit )
> {
> sessionCtx.setRollbackOnly();
> }
> }
> }
>
> The issue here is that if I set a debug breakpoint at the line with mapper.insertUser(
> user ),I sometimes see the update by referenceToB.methodInB() get commited
> to the database!! (checked via a DB2 SQL console) This happened late
> Friday and I have not had a chance to look at since. I think the culprit
> here is GlassFish, I suspect that this happens only during debugging (need
> to verify this).
>
> On a final note, I have used iBATIS 2 on a previous project and iBATIS 3 on
> the current one. Even with IBATIS 2 I thought it was an incredible piece of
> work, well-thought out and a pleasure to use. Especially love the support
> for dynamic SQL, and the type handler callbacks. Excellent work!
>
> Best,
>
> Adi
>
> --
> Acciente, LLC
> Systems Architecture and Software Design
>
> www.acciente.com
> www.inductionframework.org
>
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Adinath <ad...@acciente.com>.
Clinton,
On Sun, Apr 18, 2010 at 2:45 PM, Clinton Begin <cl...@gmail.com>wrote:
> You did the right thing... iBATIS transaction managers are meant to be easy
> to implement yourself.
>
Yes, they are easy to implement!
That said, this was a matter of constant debate back in the good old days
> when none of the standards were implemented consistently. I'm still not
> sure if they are.
>
> Some app servers would complain if iBATIS closed the connection, because if
> some downstream process needed a connection, theoretically it was supposed
> to use the same one (which would be closed by that time). I think the trick
> is to ensure that you're using a container managed DataSource. Are you
> retrieving a DataSource configured by GlassFish from a JNDI context (in
> other words, a container manged DataSource)? Or are you just using the
> iBATIS built-in SimpleDataSource?
>
Yes I am using using a container managed datasource (GlassFish v3 managing a
DB2 v9.7 connection), looked up via JNDI. I create the iBATIS session
factory using SqlSessionFactoryBuilder and the related classes (i.e. no XML
config file).
I
> I would expect a container managed DataSource to lazily initialize a
> connection for any one worker thread, and continue to provide that same
> connection to any requester for that particular DataSource. Then I'd expect
> it to close that connection at the end of the entire workflow.
>
My experience with GlassFish is that the connection needs to be closed to be
returned to the pool.
With GlassFish I wrote a set of tests and established that if you call
commit() on a JDBC connection returned by the container managed datasource
it causes an commit (thru to the driver), defeating the EJB transaction.
There are 2 other issues I ommitted in my original post, for clarity. I
suspect these 2 issues have nothing to do with IBATIS, but I am just sharing
FYI (just in case).
*Issue #1*
In the close() method of my custom iBATIS transaction class I would
transiently (about every 4 times) get IllegalStateException with
"state.isBusy() : false" in the message when I call connection.close(). Some
posts pointed that this happens when the connection is already closed. But I
have verified using the debugger that this does not apply, the connection is
open and isClosed() returns false. The fix for now is to catch the
IllegalStateException the close() and ignore it! :(
*Issue #2*
The other issue is rather unnerving, I am reluctant to share it since it is
a little winded, but thought perhaps it may help someone else. This too I
suspect has nothing to do with iBATIS. It is as follows:
- there is a stateful session bean (sfsb) A that has a reference to another
sfsb B
- there is a method in sfsb A, say methodOfA, which inserts a row using
iBATIS
- there is a method in sfsb B, say methodOfB, which inserts a row using
direct JDBC
- methodOfB() also uses a container managed DataSource to retrieve a
connection and does connection.close() before returning
- now the logic in methodOfA() looks like below:
public void methodOfA()
{
session = sessionFactory.openSession()
boolean commit = false;
try
{
mapper = session.getMapper( UserMapper.class);
someID = beanB.methodOfB();
// set value for user
user = new User();
user.setOtherStuff() ...
user.setSomeID( someID );
// insert row for new user
mapper.insertUser( user );
commit = true;
}
finally
{
session.close();
if ( ! commit )
{
sessionCtx.setRollbackOnly();
}
}
}
The issue here is that if I set a debug breakpoint at the line with
mapper.insertUser(
user ),I sometimes see the update by referenceToB.methodInB() get commited
to the database!! (checked via a DB2 SQL console) This happened late Friday
and I have not had a chance to look at since. I think the culprit here is
GlassFish, I suspect that this happens only during debugging (need to verify
this).
On a final note, I have used iBATIS 2 on a previous project and iBATIS 3 on
the current one. Even with IBATIS 2 I thought it was an incredible piece of
work, well-thought out and a pleasure to use. Especially love the support
for dynamic SQL, and the type handler callbacks. Excellent work!
Best,
Adi
--
Acciente, LLC
Systems Architecture and Software Design
www.acciente.com
www.inductionframework.org
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Jeff Butler <je...@gmail.com>.
AFAIK, it's always been required to close the connection to return it
to the connection pool. iBATIS2's EXTERNAL transaction manager does
close the connection. How else would the container know when we're
finished with the connection?
The problem I remember was with WebSphere when we didn't call commit()
before closing the connection with the JNDI transaction manager - that
caused a problem with WebSphere connection pooling and we had to
introduce the "commitRequired" property.
I did a quick look for EJB 3.1 and this:
http://java.sun.com/javaee/6/docs/tutorial/doc/bncjj.html
It seems to imply that we still have to call close to return the
connection to the pool.
Jeff Butler
On Sun, Apr 18, 2010 at 4:45 PM, Clinton Begin <cl...@gmail.com> wrote:
> You did the right thing... iBATIS transaction managers are meant to be easy
> to implement yourself.
>
> That said, this was a matter of constant debate back in the good old days
> when none of the standards were implemented consistently. I'm still not
> sure if they are.
>
> Some app servers would complain if iBATIS closed the connection, because if
> some downstream process needed a connection, theoretically it was supposed
> to use the same one (which would be closed by that time). I think the trick
> is to ensure that you're using a container managed DataSource. Are you
> retrieving a DataSource configured by GlassFish from a JNDI context (in
> other words, a container manged DataSource)? Or are you just using the
> iBATIS built-in SimpleDataSource?
>
> I would expect a container managed DataSource to lazily initialize a
> connection for any one worker thread, and continue to provide that same
> connection to any requester for that particular DataSource. Then I'd expect
> it to close that connection at the end of the entire workflow.
>
> Does anyone know if the spec specifies this?
>
> Cheers,
> Clinton
>
> On Sun, Apr 18, 2010 at 2:31 PM, Adinath <ad...@acciente.com> wrote:
>>
>> Is anybody out there using iBATIS 3 as their persistence framework inside
>> an EJB container? I recently started building a new system for which I
>> choose EJB 3.1 (the version of EJB is actually irrelevant to this question)
>> as my app framework and iBATIS 3 (this version is relevant!) as my
>> persistence framework. My business logic is implemented in EJB 3.1 session
>> beans that use iBATIS 3 for data access. I am running on GlassFish v3)
>>
>> My issue with this stack has been transaction management. I have solved my
>> problem by writing some simple integration code, but I was little surprised
>> I had to do this. So I decided to post this to see if others have run into
>> this and if so how they solved the problem.
>>
>> My requirement is for iBATIS 3 to transparently use the EJB transaction
>> (typically defined declaratively) in the session bean method. iBATIS 3
>> provides 2 transaction factories JdbcTransactionFactory and
>> ManagedTransactionFactory and I found that neither one works correctly in an
>> EJB environment (and looking at the iBATIS source it is clear why it fails).
>>
>> The JdbcTransactionFactory is inappropriate since I want any calls to
>> sqlSession.commit() or sqlSession.rollback() to be ignored. So I thought ok
>> I should use the ManagedTransactionFactory since it does cause any calls to
>> sqlSession.commit() or sqlSession.rollback() to be ignored, however it also
>> causes sqlSession.close() to not close the connection that iBATIS opened
>> from the DataSource in sqlSession.open() (the DataSource is a container
>> managed DataSource object I provide to iBATIS). This causes the GlassFish to
>> exhaust its connect pool and the application fails.
>>
>> So I wrote a new TransactionFactory implementation, EJBTransactionFactory,
>> that causes sqlSession.commit() or sqlSession.rollback() to do nothing, but
>> does close the connection when sqlSession.close() is called.
>>
>> I suspect other people have run into this, how did you solve it?
>>
>> Thanks,
>> Adi
>>
>> --
>> Acciente, LLC
>> Systems Architecture and Software Design
>>
>> www.acciente.com
>> www.inductionframework.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-java-unsubscribe@ibatis.apache.org
For additional commands, e-mail: user-java-help@ibatis.apache.org
Re: Transaction management in iBATIS 3 inside an EJB (3.1) container
Posted by Clinton Begin <cl...@gmail.com>.
You did the right thing... iBATIS transaction managers are meant to be easy
to implement yourself.
That said, this was a matter of constant debate back in the good old days
when none of the standards were implemented consistently. I'm still not
sure if they are.
Some app servers would complain if iBATIS closed the connection, because if
some downstream process needed a connection, theoretically it was supposed
to use the same one (which would be closed by that time). I think the trick
is to ensure that you're using a container managed DataSource. Are you
retrieving a DataSource configured by GlassFish from a JNDI context (in
other words, a container manged DataSource)? Or are you just using the
iBATIS built-in SimpleDataSource?
I would expect a container managed DataSource to lazily initialize a
connection for any one worker thread, and continue to provide that same
connection to any requester for that particular DataSource. Then I'd expect
it to close that connection at the end of the entire workflow.
Does anyone know if the spec specifies this?
Cheers,
Clinton
On Sun, Apr 18, 2010 at 2:31 PM, Adinath <ad...@acciente.com> wrote:
> Is anybody out there using iBATIS 3 as their persistence framework inside
> an EJB container? I recently started building a new system for which I
> choose EJB 3.1 (the version of EJB is actually irrelevant to this question)
> as my app framework and iBATIS 3 (this version is relevant!) as my
> persistence framework. My business logic is implemented in EJB 3.1 session
> beans that use iBATIS 3 for data access. I am running on GlassFish v3)
>
> My issue with this stack has been transaction management. I have solved my
> problem by writing some simple integration code, but I was little surprised
> I had to do this. So I decided to post this to see if others have run into
> this and if so how they solved the problem.
>
> My requirement is for iBATIS 3 to transparently use the EJB transaction
> (typically defined declaratively) in the session bean method. iBATIS 3
> provides 2 transaction factories JdbcTransactionFactory and
> ManagedTransactionFactory and I found that neither one works correctly in an
> EJB environment (and looking at the iBATIS source it is clear why it fails).
>
> The JdbcTransactionFactory is inappropriate since I want any calls to
> sqlSession.commit() or sqlSession.rollback() to be ignored. So I thought ok
> I should use the ManagedTransactionFactory since it does cause any calls to
> sqlSession.commit() or sqlSession.rollback() to be ignored, however it also
> causes sqlSession.close() to *not* close the connection that iBATIS opened
> from the DataSource in sqlSession.open() (the DataSource is a container
> managed DataSource object I provide to iBATIS). This causes the GlassFish to
> exhaust its connect pool and the application fails.
>
> So I wrote a new TransactionFactory implementation, EJBTransactionFactory,
> that causes sqlSession.commit() or sqlSession.rollback() to do nothing, but
> *does close* the connection when sqlSession.close() is called.
>
> I suspect other people have run into this, how did you solve it?
> Thanks,
> Adi
>
> --
> Acciente, LLC
> Systems Architecture and Software Design
>
> www.acciente.com
> www.inductionframework.org
>