Consider the following code:
public class Foo {
private static final String TEST_VENDOR = "testVendor";
@Resource
private ModelService modelService;
@Resource
private FlexibleSearchService flexibleSearchService;
private CyclicBarrier barrier = new CyclicBarrier(2);
public void foo() throws InterruptedException, BrokenBarrierException {
barrier.await();
Transaction.current().begin();
final VendorModel vendor = modelService.create(VendorModel.class);
vendor.setCode(TEST_VENDOR);
modelService.save(vendor);
Transaction.current().commit();
barrier.await();
// bar queries testVendor
}
public void bar() throws InterruptedException, BrokenBarrierException {
barrier.await();
// foo creates testVendor
barrier.await();
final VendorModel otherVendor =
flexibleSearchService.<VendorModel> search("select * from {Vendor} where {code} = 'testVendor'").getResult().get(0);
Assert.assertNotNull(otherVendor);
}
}
I define Foo
as a spring bean and invoke the methods bar
and foo
via hac groovy scripts from two different hac sessions of different browsers. For the groovy console I define execution with commit and do a Transaction.current().commit()
before invoking a method of Foo
to terminate the transaction started by the groovy console.
When the code is run, an AssertionError
is thrown in bar()
because otherVendor
is null
. However when I let the thread executing bar stop at a breakpoint before the flexible search service invocation and do a hmc search or a search via the hac flexible search console I do find the Vendor with code testVendor
.
Why doesn't the thread that is executing the bar()
method return the instance of Vendor with code testVendor
?
Entity caching is enabled. Hybris platform version is 5.7.0.17 running on Windows 7 x64. All hybris operations are done using the same admin user.
Just for reference here are the groovy scripts:
script for invocation of Foo.foo()
:
import de.hybris.platform.core.Registry;
import de.hybris.platform.tx.Transaction;
try {
Transaction.current().commit();
final Foo bean =
Registry.getApplicationContext().getBean("foo", Foo.class);
bean.foo();
} catch (final Throwable e) {
e.printStackTrace();
throw e;
}
script for invocation of Foo.bar()
:
import de.hybris.platform.core.Registry;
import de.hybris.platform.tx.Transaction;
try {
Transaction.current().commit();
final Foo bean =
Registry.getApplicationContext().getBean("foo", Foo.class);
bean.bar();
} catch (final Throwable e) {
e.printStackTrace();
throw e;
}