java - Spring Tests : transaction not rolling back after test method executed -
i'm trying create integration tests legacy application deployed on weblogic 8.1 using subclass of abstracttransactionaljunit4springcontexttests.
my test method has following annotations :
@test @rollback(true) public void testdeployedejbcall throws exception {...}
my test class references beans of type org.springframework.ejb.access.simpleremotestatelesssessionproxyfactorybean, proxy ejbs deployed on weblogic server.
when call methods on proxy bean in sequencial manner in test method, transaction rolls correctly @ end of test.
e.g. :
@test @rollback(true) public void testdeployedejbcall throws exception { long result1 = myejb.method(100l); long result2 = myejb.method(200l); ... }
however, make 2 parallel calls same ejb method. therefore i've made inner class implements callable, in order call methods in 2 different threads , hope run in parallel.
however, doing seems make ejb methods called outside transaction, , nothing rolled back.
here full test class when run method calls in parallel :
import org.springframework.test.annotation.*; @runwith(springjunit4classrunner.class) @transactional @contextconfiguration(locations = {"classpath:path/to/tests-config.xml"}) @transactionconfiguration(defaultrollback=true) public final class integrationtests extends abstracttransactionaljunit4springcontexttests { @autowired protected jnditemplate jnditemplate; @resource protected proxy myejb; public integrationtests() { super(); this.logger = logger.getlogger(integrationtests.class); } @test @rollback(true) public void testdeployedejbcall() throws exception { // create thread pool parallel execution. executorservice exec = executors.newfixedthreadpool(2); // prepare tasks parallel execution list<callejbtask> tasks = new arraylist<callejbtask>(); tasks.add(new callejbtask(100l, this.myejb)); tasks.add(new callejbtask(200l, this.myejb)); // execute pending tasks in exec threadpool list<future<long>> results = exec.invokeall(tasks); // results of each task long result1 = results.get(0).get(); long result2 = results.get(1).get(); ... } } private class callebjtask implements callable<long> { private final long valuetotest; private final myejb myejb; public callejbtask(long valuetotest, proxy myejbproxy) this.valuetotest = valuetotest; this.myejb = (myejb)myejbproxy; } public long call() throws exception { return getresult(); } public long getresult() { long result = null; try { result = this.myejb.method(this.patient); } catch (exception e) { ... } return result; } }
is there way make rollback ???
thanks help.
regards,
philippe
not automatically, no. problem 2 threads don't participate in transaction, hence actions don't rollback.
what purpose of 2 parallel executions? unlikely able test concurrency issues approach, if you're aiming for.
edit: problem testing concurrency issues hard, because tests are, @ best, probabilistic – success or failure depend on subtle timing issues may surface on billionth run. see this serverside article summary of basics.
the rule of thumb should avoid hand-coding threading whenever possible, hard right , difficult test. if can, avoid shared state between threads, , if there no way around it, rely on concurrent data structures , asynchronous executors java.util.concurrent
package.
Comments
Post a Comment