java - What exactly is meant by Spring transactions being atomic? -
my understanding of atomic operation should not possible steps of operation interleaved of other operation - should executed single unit.
i have method creating database record first of check if record same value, satisfies other parameters, exists, , if not create record.
in fakecode:
public class foodao implements ifoodao { @transactional public void createfoo(string foovalue) { if (!fooexists(foovalue)) { // db call create foo } } @transactional public boolean fooexists(string foovalue) { // db call check if foo exists } }
however have seen possible 2 records same value created, suggesting these operations have interleaved in way. aware spring's transactional proxies, self-invocation of method within object not use transactional logic, if createfoo() called outside object expect fooexists() still included in same transaction.
are expectations transactional atomicity should enforce wrong? need using synchronized block enforce this?
what transaction mean database depends on isolation level. wikipdia article on isolation (database systems) explain well.
normally 1 use not high isolation level, example: read committed
. mean 1 can read data other transaction not until other transaction committed. in case not enough, because opposite want. - obvious solution using more restrictive , slower isolation level: repeatable reads
.
but honest, use other way: make relevant column unique (but not remove if (!fooexists(foovalue))
-check). in 99% check work. in remaining 1% exception, because try violate unique constraint.
Comments
Post a Comment