Hi,
Is there a way to open and operate a new ISession with user-provided IDbConnection that currently is in a transaction? It seems that NHibernate does not "join" this transaction (sql commands are not enlisted with it).
Example (probably not a good practice, but anyway):
Code:
ISession session = sessionFactory.OpenSession();
ITransaction tx = session.BeginTransacton();
// Now I need another ISession on the same IDbConnection
ISession anotherSession = session.SessionFactory.OpenSession(session.Connection);
When NHibernate tries to execute sql commands using anotherSession, there is an error message stating that the command is not enlisted with an active transaction.
The reason I need anotherSession is that I'm trying to select some entity using Native SQL from database table that is different (but has the same structure) as the one that this entity is mapped to. (History table vs. Actual table). NHibernate confuses this selected entity with one that is already in session cache (has the same type and id, but came from different table). To make things more complicated, there is joined-sublass inheritance involved as well.
Code:
StringBuilder sql = new StringBuilder()
.AppendLine("SELECT {m.*}")
.AppendLine("FROM Muzeji_History m JOIN MuzejaVienibas_History m_1_ ON m_1_._AuditaIeraksts = m._AuditaIeraksts")
.AppendLine("WHERE m._AuditaIeraksts = :id");
ISQLQuery query = session.CreateSQLQuery(sql.ToString())
.AddEntity("m", typeof(Muzejs));
return (Muzejs)query.SetParameter("id", id, NHibernateUtil.Guid).UniqueResult();
In the mappings 'Muzejs' type is mapped to a different set of tables.
That's why I tried a solution with a temporary session for this read-only select. I have to use the same connection because of locking issues.
Any ideas?
Thanks,
Andy