开发者

Committing a database change in isolation to the main transaction

Let's say I have the following code:

public void doSomething() {
    // begin transaction

    // do stuff 1
    ...

    // update a row which must be committed now
    ...

    // do stuff 2
    ...

    // commi开发者_如何学编程t transaction
}

I've kept the large amount of work on the database in the one method to simplify the pseudo code. However, basically I have a series of database work that I won't want to commit until the end. In the middle of the transaction above, I need to commit something to a row without committing the rest of the transaction. How would I commit that small bit of work without affecting the rest of the transaction?


Usually you accomplish this simply by grabbing another connection and performing the work on the other database connection. You may or may not need to manage database isolation levels to have the visibility you desire. This is how, for example Propagation.REQUIRES_NEW works in Spring. (edit: one notes you tagged the question Hibernate. Essentially, you make an entirely new session/entitymanager and do the separate work with that one. This will have some probably unexpected results for object state across the boundaries. If you load something from one session and try to keep using it across the boundary into the other it's liable to explode on you. You will need to be careful to manage it properly and keep track of what is managed by which Session.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜