Reputation: 7647
In the below article says,
PROPAGATION_REQUIRED – Support a current transaction; create a new one if none exists.
Below code inert a product and then product details for two tables.
public void save(Product product, int qoh){
productDao.save(product);
System.out.println("Product Inserted");
ProductQoh productQoh = new ProductQoh();
productQoh.setProductId(product.getProductId());
productQoh.setQty(qoh);
productQohBo.save(productQoh);
System.out.println("ProductQoh Inserted");
}
My problem is when this kind of behavior can happen? I mean How come a current transaction ends? Is it after a save or update?
If we use PROPAGATION_REQUIRED suppose the current transaction ends after insert product. Then a new transaction comes but if any failure when insert product quantity it will only rollback the quantity not the product details entered know? So still the data is inconsistency knw?
Upvotes: 0
Views: 157
Reputation: 6540
In your example, if an exception is thrown (because incorrect data was entered) the entire method would roll back leaving you with nothing inserted. This is assuming that you have annotated the save
method @Transactional
. If you annotate your DAO methods @Transactional
then you would get some objects being inserted and some not. It is better to keep your annotations at the Service
layer so that you know that your database will not be dirtied by partial failures.
TL;DR: Either everything is saved to the database or nothing is saved to the database.
Upvotes: 1
Reputation: 1411
It depends on where you have applied Transaction declarative rules.
I suppose the example provided is from Service
layer. If it is so, it would be wise to apply the transaction declarations to the service layer to maintain the integrity.
Upvotes: 0