cancel
Showing results for 
Search instead for 
Did you mean: 

Why does the query result contain a null when the item was created just before by another thread?

Former Member
0 Kudos

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;
 }

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

The problem was the flexible search query

 select * from {Vendor} where {code} = 'testVendor'

This resulted in SQL that started with:

 SELECT * FROM vendors item_t0

Executing this via flexible search direct SQL console resulte in the HJMPTS column being first. This was then interpreted as the primary key.

I also received the exception java.lang.IllegalArgumentException: invalid pks [1] - unknown typecode 0] when running the flexible search select * from {Vendor} in a groovy script. The HJMPTS value 1 was interpreted as a pk in this case.

Answers (0)