Am1rr3zA
Am1rr3zA

Reputation: 7421

Hibernate multi level of transaction

I have some hibernate code and I want my code run in 1 transaction let me explain in code

public void changeBranch(Branch branch) throws DatabaseException {
//some code
            humanDao.update(he);
            superBranchUsername = branch.getFatherUsername();
            int superBranchId = branchDao.getBranchIdByUserName(superBranchUsername);
            BranchEntity superBranch = branchDao.load(superBranchId);
            BranchEntity be = new BranchEntity();
            setBranchEntity(be, he, pkId, bname, confirmed, level, studentCount, uname, superBranch);
            branchDao.update(be);   // update kardane jadvale Branch va Set kardane Human motenazer be on
//some code
}

Both humanDao.update(he); and branchDao.update(be); run in transaction handle by My GenericDAO that humanDao and branchDao are inherited from it. but I want this block of code (wrote above) to also run in a transaction!! How can I get to Hibernate to do this?

Upvotes: 0

Views: 762

Answers (3)

Am1rr3zA
Am1rr3zA

Reputation: 7421

I find how should I fix it if I new session in changeBranch(Branch branch) and pass this session as a parameter to my DAO my problem solved

Upvotes: 0

duffymo
duffymo

Reputation: 308763

DAOs should not handle transactions for exactly the reason you've discovered: they can't know when they're part of a larger transaction.

If you were using Spring declarative transactions, you'd have a service layer that would create the transaction context for both DAOs and deal with everything. I would recommend doing something like that.

UPDATE: I added a link to Spring.

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300549

Please see: Chapter 11. Transactions and Concurrency

Upvotes: 1

Related Questions