I have a situation where I need to manage NHibernate connections/
transaction so that I can share transactions between NHibernate and
our legacy data access methods. What I have done is created a custom
OraclConnectionProvider that returns a wrapped OracleConnection.
The problem is, when I use Session.CreateSQLQuery("..."), the Oracle
driver throws an InvalidCastException:
Unable to cast object of type 'DataPersistence.DbConnectionWrapper' to
type 'Oracle.DataAccess.Client.OracleConnection'.
at
Oracle.DataAccess.Client.OracleCommand.System.Data.IDbCommand.set_Connection
(IDbConnection value)
Any ideas how to move past this?
Here's an example of code that exhibits this problem (but is too
simple to actually solve the transaction sharing problem):
public class CustomConnectionProvider : DriverConnectionProvider
{
public override IDbConnection GetConnection()
{
return OracleConnectionWrapper.Create(base.GetConnection());
}
public override void CloseConnection(IDbConnection connection)
{
base.CloseConnection(connection);
}
}
public class OracleConnectionWrapper : IDbConnection
{
public OracleConnection Connection { get; set; }
public TransactionWrapper Transaction { get; set; }
public static OracleConnectionWrapper Create(string key,
OracleConnection baseConnection)
{
return new OracleConnectionWrapper(baseConnection);
}
private OracleConnectionWrapper(OracleConnection connection)
{
Connection = connection;
Transaction = null;
}
public static implicit operator OracleConnection
(OracleConnectionWrapper self)
{
return self.Connection;
}
public IDbTransaction BeginTransaction(IsolationLevel il)
{
Transaction = new TransactionWrapper
(Connection.BeginTransaction(il));
return Transaction;
}
public IDbTransaction BeginTransaction()
{
Transaction = new TransactionWrapper
(Connection.BeginTransaction());
return Transaction;
}
// all the other implementation essentially calls
Connection.whatever();
}
public class TransactionWrapper : IDbTransaction
{
public IDbTransaction Transaction { get; set; }
public TransactionWrapper(IDbTransaction txn)
{
Transaction = txn;
}
public void Commit()
{
Transaction.Commit();
var wrappedConnection = Transaction.Connection as
OracleConnectionWrapper;
wrappedConnection.Transaction = null;
}
public void Rollback()
{
Transaction.Rollback();
var wrappedConnection = Transaction.Connection as
OracleConnectionWrapper;
wrappedConnection.Transaction = null;
}
// all the other implementation essentially calls
Transaction.whatever();
}
Thanks!
-Eric.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---