强制查询执行不冲水/提交 [英] Force query execution without flush/commit

查看:126
本文介绍了强制查询执行不冲水/提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个asp.net web应用程序事务每请求(会话在视图)模式。我有一对夫妇在这里我要救一个NHibernate的管理实体,然后做一对夫妇使用普通SQL的详细插入和更新的应用程序中的要点。这些插入/更新依赖于该NH保存的实体将采取ID。

Hi i am using the transaction-per-request (session-in-view) pattern for an asp.net web application. I have a couple of points in the application where i want to Save an NHibernate managed entity and then do a couple of more inserts and updates using common sql. These inserts/updates depend on the ID that the NH saved entity will take.

的问题是,所生成的ID不存在于交易'范围存在。如果我强迫刷新/提交ID被持久,但如果插入/更新失败,​​我必须回滚,但刷新/提交的实体不会。目前我做的这些情况下,手动插入但这是我要改变。那么,有没有保存(后执行SQL语句(已打开事务中)),但没有强制的方式刷新/提交?

The problem is that the generated id does not exist in the transactions' scope. If i force a flush/commit the id is persisted but if the inserts/updates fail i have to rollback but the flushed/committed entity will not. Currently I'm doing a manual insert for these cases but that is something i want to change. So, is there a way to execute the SQL statement (inside the already open transaction) after the Save() but without forcing a flush/commit?

编辑:我加入一个半伪code的例子,我有4错误的答案,所以我认为人们不明白(NHibernate的是如何工作的)
在开始要求我发出了一个

I'm adding a semi-pseudocode example, i got 4 wrong answers so i think people don't understand (how NHibernate works) At the Begin request i issue a

nhsession.BeginTransaction()

那么在某些时候我做

then at some point i do

FooClass fc = new FooClass("value");
nhsession.Save(fc);
ITransaction trans = nhsession.Transaction;
SqlCommand sc = new SqlCommand("some insert/update query that depends on fc's id", (SqlConnection)nhsession.Connection);
sc.Parameters.Add("id", fc.Id); //NHibernate generates the id, note i'm using assigned/hi-lo so no round trip to the db takes place
transaction.Enlist(sc);
try {
    sc.ExecuteNonQuery();
}
catch (SqlException ex){
    transaction.RollBack();
    nhsession.Close();
}

,并在请求结束时,我发出 CommitTransaction() nhsession.Close()

现在这会什么都不做:在 FooClass(FC)尚未刷新/ COMMITED到数据库中。该NH做了保存()操作达内存这一点。这意味着没有SQL命令已被NHibernate的发行,这意味着,在的SqlCommand(SC)我解雇之后就会失败作为ID不存在。

Now this will do absolutely nothing: the FooClass (fc) has not been flushed/commited to the database. The Save() operation that NH has done is up to that point in-memory. That means no sql command has been issued by nhibernate and that means that the SqlCommand (sc) that i fire afterwards will fail miserably as the id does not exist.

如果我做了冲洗/ 之间提交保存()的SqlCommand FooClass(FC) _cannot_be_rolled_back_,这是一个坏人坏事。
目前,这个工作我做使用的SqlCommand vanila SQL INSERT,我想改变这种状况。 (为什么?因为我不想做香草插入它们很容易受到由于模式/模式的变化的错误,我得到了OR / M为)

If i do a flush/commit between Save() and the SqlCommand the FooClass(fc) _cannot_be_rolled_back_ and that is a bad bad thing. Currently, for this to work i make vanila sql insert using an SqlCommand, and i want to change that. (Why? because i don't want to make vanilla inserts they are susceptible to errors due to schema/model changes, and i got the OR/M for that)

如何?我想以某种方式通知NHibernate的执行的SqlCommand 来对应于保存()插入(地狱,它可以做所有SqlCommands它已经聚集了),但没有它commiting或冲洗!

How? i want to notify NHibernate somehow to execute the SqlCommand to corresponds to the Save() insert (hell, it can do all the SqlCommands it has gathered) but without it commiting or flushing!.

目前我也在寻找冲洗/ commiting保存的对象时NHibernate的产生prepared SQL语句。也许我可以只取该字符串,在我的SqlCommand是在事务中登记运行。

Currently i'm also searching for the prepared sql statement that nhibernate produces when flushing/commiting a saved object. Maybe i can just take that string and run it in my SqlCommand that is enlisted in the Transaction.

推荐答案

也许我不理解,但你不能做这样的...

maybe I don't understand but can't you do it like so...

using(var tx = session.BeginTransaction())
{
    session.Save(entity);
    session.Flush();

    //perform non NH operations with entity.id

    tx.Commit();
}//uncommitted transaction would be rolled back on dispose()

这篇关于强制查询执行不冲水/提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆