TransactionException: The operation is not valid for state of transaction

6,948 views
Skip to first unread message

Samir

unread,
Feb 19, 2011, 7:33:37 AM2/19/11
to nhusers
We are having an application which is working file in development
environment, but in the test environment, we get an exception whenever
we try to perform second operation within a transaction scope.
Pseudocode that is causing the issue:

using (TransactionScope tScope = new TransactionScope())
{
using (ISession session = sessionFactory.OpenSession())
{
session.SaveOrUpdate(obj1); // some transient obj
session.Flush();
}

using (ISession session = sessionFactory.OpenSession())
{
session.SaveOrUpdate(obj2); // some another transient obj
session.Flush(); // Exception occurs here
}
}

The exception that occurs:

Message: Error has occured in service:
System.Transactions.TransactionException: The operation is not valid
for the state of the transaction.
at
System.Transactions.TransactionState.EnlistVolatile(InternalTransaction
tx, IEnlistmentNotification enlistmentNotification, EnlistmentOptions
enlistmentOptions, Transaction atomicTransaction)
at
System.Transactions.Transaction.EnlistVolatile(IEnlistmentNotification
enlistmentNotification, EnlistmentOptions enlistmentOptions)
at
NHibernate.Transaction.AdoNetWithDistributedTransactionFactory.EnlistInDistributedTransactionIfNeeded(ISessionImplementor
session)
at
NHibernate.Impl.AbstractSessionImpl.EnlistInAmbientTransactionIfNeeded()
at
NHibernate.Impl.AbstractSessionImpl.CheckAndUpdateSessionStatus()
at NHibernate.Impl.SessionImpl.get_Batcher()
at NHibernate.Type.DbTimestampType.UsePreparedStatement(String
timestampSelectString, ISessionImplementor session)
at
NHibernate.Type.DbTimestampType.GetCurrentTimestamp(ISessionImplementor
session)
at NHibernate.Type.DbTimestampType.Seed(ISessionImplementor
session)
at NHibernate.Type.TimestampType.Next(Object current,
ISessionImplementor session)
at NHibernate.Engine.Versioning.Increment(Object version,
IVersionType versionType, ISessionImplementor session)
at
NHibernate.Event.Default.DefaultFlushEntityEventListener.GetNextVersion(FlushEntityEvent
event)
at
NHibernate.Event.Default.DefaultFlushEntityEventListener.ScheduleUpdate(FlushEntityEvent
event)
at
NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent
event)
at
NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent
event)
at
NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent
event)
at
NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent
event)
at NHibernate.Impl.SessionImpl.Flush()

We have checked that MSDTC is running fine. On test server, if we use
plain OracleConnection class within TransactionScope, then code is
working fine. Issue occurs when using NHibernate session only. We are
using:

NHibernate 3.0.0
Oracle Client 11.2.0.1
.NET 3.5 SP1

In the nhibernate logs, following are the last few lines when
transaction is suddenly rolled back without any error in the log:

2011-02-19 19:54:38,155 [9] DEBUG NHibernate.AdoNet.AbstractBatcher -
ExecuteNonQuery took 1 ms
2011-02-19 19:54:38,155 [9] DEBUG NHibernate.AdoNet.AbstractBatcher -
Closed IDbCommand, open IDbCommands: 0
2011-02-19 19:54:38,170 [9] DEBUG
NHibernate.Persister.Collection.ICollectionPersister - done deleting
collection
2011-02-19 19:54:38,170 [9] DEBUG
NHibernate.Persister.Collection.ICollectionPersister - Inserting
collection: [XXX.BusinessEntities.RolePlayer.PartyNameList#843]
2011-02-19 19:54:38,186 [9] DEBUG
NHibernate.Persister.Collection.ICollectionPersister - collection was
empty
2011-02-19 19:54:38,186 [9] DEBUG NHibernate.AdoNet.ConnectionManager
- registering flush end
2011-02-19 19:54:38,201 [9] DEBUG
NHibernate.Event.Default.AbstractFlushingEventListener - post flush
2011-02-19 19:54:38,201 [9] DEBUG NHibernate.Impl.SessionImpl -
[session-id=4391a921-5624-4552-bfa4-13b87f92b6cc] running
ISession.Dispose()

------- Here the first session is complete and second start starts
within same TransactionScope

2011-02-19 19:54:38,217 [9] DEBUG NHibernate.Impl.SessionImpl -
[session-id=5b33510a-593c-464c-98d9-f01eb52dce24] opened session at
timestamp: 634337420782, for session factory: [/
b72244cfece246d985ae4d4a2bbd9bd3]
2011-02-19 19:54:38,217 [9] DEBUG NHibernate.Impl.AbstractSessionImpl
- enlisted into DTC transaction: Serializable
2011-02-19 19:54:38,233 [9] DEBUG NHibernate.Engine.VersionValue -
version unsaved-value strategy UNDEFINED
2011-02-19 19:54:38,248 [9] DEBUG NHibernate.Engine.IdentifierValue -
unsaved-value: 0
2011-02-19 19:54:38,248 [9] DEBUG
NHibernate.Event.Default.AbstractSaveEventListener - transient
instance of: XXX.BusinessEntities.ScreenSectionConfirmation
2011-02-19 19:54:38,264 [9] DEBUG
NHibernate.Event.Default.DefaultMergeEventListener - merging transient
instance
2011-02-19 19:54:38,280 [9] DEBUG NHibernate.AdoNet.AbstractBatcher -
Opened new IDbCommand, open IDbCommands: 1
2011-02-19 19:54:38,280 [9] DEBUG NHibernate.AdoNet.AbstractBatcher -
Building an IDbCommand object for the SqlString: select
SEQ_SCR_SECTION_CONFIRM_ID.nextval from dual
2011-02-19 19:54:38,295 [9] DEBUG NHibernate.SQL - select
SEQ_SCR_SECTION_CONFIRM_ID.nextval from dual
2011-02-19 19:54:38,295 [9] DEBUG
NHibernate.Connection.DriverConnectionProvider - Obtaining
IDbConnection from Driver
2011-02-19 19:54:38,436 [9] DEBUG NHibernate.Impl.SessionImpl -
transaction completion
2011-02-19 19:54:38,436 [9] DEBUG NHibernate.AdoNet.ConnectionManager
- aggressively releasing database connection
2011-02-19 19:54:38,451 [9] DEBUG
NHibernate.Connection.ConnectionProvider - Closing connection
2011-02-19 19:54:38,451 [9] DEBUG NHibernate.Impl.AbstractSessionImpl
- rolled back DTC transaction
2011-02-19 19:54:38,467 [9] DEBUG NHibernate.Impl.SessionImpl -
transaction completion
2011-02-19 19:54:38,467 [9] DEBUG NHibernate.AdoNet.ConnectionManager
- aggressively releasing database connection
2011-02-19 19:54:38,483 [9] DEBUG NHibernate.Impl.AbstractSessionImpl
- rolled back DTC transaction
2011-02-19 19:54:38,483 [9] DEBUG NHibernate.Impl.SessionImpl -
transaction completion
2011-02-19 19:54:38,498 [9] DEBUG NHibernate.AdoNet.ConnectionManager
- aggressively releasing database connection
2011-02-19 19:54:38,498 [9] DEBUG NHibernate.Impl.SessionImpl -
[session-id=4391a921-5624-4552-bfa4-13b87f92b6cc] executing real
Dispose(True)
2011-02-19 19:54:38,514 [9] DEBUG NHibernate.Impl.SessionImpl -
closing session
2011-02-19 19:54:38,514 [9] DEBUG NHibernate.AdoNet.AbstractBatcher -
running BatcherImpl.Dispose(true)
2011-02-19 19:54:38,530 [9] DEBUG NHibernate.Impl.SessionImpl -
transaction completion
2011-02-19 19:54:38,545 [9] DEBUG NHibernate.AdoNet.ConnectionManager
- aggressively releasing database connection
2011-02-19 19:54:38,561 [9] DEBUG NHibernate.Impl.AbstractSessionImpl
- enlisted into DTC transaction: Serializable
2011-02-19 19:54:38,561 [9] DEBUG NHibernate.Impl.SessionImpl -
transaction completion
2011-02-19 19:54:38,576 [9] DEBUG NHibernate.AdoNet.ConnectionManager
- aggressively releasing database connection
2011-02-19 19:54:38,576 [9] DEBUG NHibernate.Impl.SessionImpl -
[session-id=5b33510a-593c-464c-98d9-f01eb52dce24] running
ISession.Dispose()
2011-02-19 19:54:38,592 [9] DEBUG NHibernate.Impl.SessionImpl -
[session-id=5b33510a-593c-464c-98d9-f01eb52dce24] executing real
Dispose(True)
2011-02-19 19:54:38,592 [9] DEBUG NHibernate.Impl.SessionImpl -
closing session
2011-02-19 19:54:38,608 [9] DEBUG NHibernate.AdoNet.AbstractBatcher -
running BatcherImpl.Dispose(true)
2011-02-19 19:54:38,608 [9] DEBUG NHibernate.AdoNet.AbstractBatcher -
Closed IDbCommand, open IDbCommands: 0

Juan

unread,
Feb 21, 2011, 9:13:19 PM2/21/11
to nhusers
Since you didn't call transactioscope.complete(), it is trying to
rollback the SQL insert/updates issued by session.flush(), but since
the session is gone, it can't

Samir

unread,
Feb 22, 2011, 1:41:56 AM2/22/11
to nhusers
Hi Juan,

It was mistake in my mail. We do have TransactionScope.complete() just
before end of using block. Problem is it does not reach there at all.
The exception occurs before that - second session.flush().

Regards,
Samir

Juan

unread,
Feb 22, 2011, 11:09:49 PM2/22/11
to nhusers
Hmm... connection pooling and oraclemts winservice enabled?

Also, have you seen this? It might be related
http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-nhibernate-and-transactionscope/

syva...@gmail.com

unread,
Feb 23, 2011, 1:26:46 AM2/23/11
to nhu...@googlegroups.com
Yes, both pooling and OraMTS are enabled. We have tried reinstalling COM+ and MSDTC too. But no luck.

I've seen the link that you mentioned. But it does not help much. It is mainly to avoid leaking connections - if we don't use NH transactions within transactionscope, then it leaks connections. Unfortunately for us we don't reach that stage at all. Exception occurs before end of transactionScope block.
> --
>
> You received this message because you are subscribed to the Google Groups "nhusers" group.
>
> To post to this group, send email to nhu...@googlegroups.com.
>
> To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
>
> For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
>
>
>

Juan

unread,
Feb 24, 2011, 11:11:37 AM2/24/11
to nhusers
Just recreated your scenario, but everything works for me. Compared
the logs and they both look pretty much the same until your 2nd
insert, when it tries to get the id via a sequence, yours bomb...
probably connection is not valid.

At this point it seems like a configuration problem (nh, connection
string, msdtc, etc)

Can you post your nh configuraiton?

On Feb 23, 1:26 am, syvai...@gmail.com wrote:
> Yes, both pooling and OraMTS are enabled. We have tried reinstalling COM+  
> and MSDTC too. But no luck.
>
> I've seen the link that you mentioned. But it does not help much. It is  
> mainly to avoid leaking connections - if we don't use NH transactions  
> within transactionscope, then it leaks connections. Unfortunately for us we  
> don't reach that stage at all. Exception occurs before end of  
> transactionScope block.
>
> On , Juan <juan.gree...@gmail.com> wrote:
>
> > Hmm... connection pooling and oraclemts winservice enabled?
> > Also, have you seen this? It might be related
> >http://davybrion.com/blog/2010/05/avoiding-leaking-connections-with-n...
> ...
>
> read more »

Samir Vaidya

unread,
Feb 25, 2011, 7:38:30 AM2/25/11
to nhu...@googlegroups.com, Juan
Hi Juan,

We fixed the issue today. We did a clean reinstall of MSDTC based on Microsoft KB article. Now the same code is working fine!

Thanks for your interest.

Regards,
Samir


--
Reply all
Reply to author
Forward
0 new messages