RE: LocalServiceUtil.getCount return a old value after insert a new record?

thumbnail
Tinh Nguyen, modified 7 Years ago. Junior Member Posts: 31 Join Date: 5/10/18 Recent Posts

Hi all,
I have a problem. When i change database such as insert a new record then LocalService still get a old version of database.

For example:

Table A have 10 records. I add 2 new records and use LocalServiceUtil.getCount in portlet then i still get 10 result.

What 's happening?

P/S: I added by handle in msql workbench.

Please help me.

 

Thanks for reading

Tinhnx

Ravi Shah, modified 7 Years ago. Junior Member Posts: 59 Join Date: 1/2/14 Recent Posts

Can you please confirm if you have manually inserted record in database? If yes, try Clear Database Cache option under server administration. 

thumbnail
Samuel Kong, modified 7 Years ago. Liferay Legend Posts: 1902 Join Date: 3/10/08 Recent Posts

How did you add the 2 records? Did you add it manually or did you use LocalServiceUtil to add. If you're not using LocalServiceUtil to add, there's no way for the portal to know that it needs to update it's cache.

thumbnail
Tinh Nguyen, modified 7 Years ago. Junior Member Posts: 31 Join Date: 5/10/18 Recent Posts

Hi Samuel Kong and Ravi Shah,

I did insert new by LocalServiceUtil.update...

Ravi Shah, modified 7 Years ago. Junior Member Posts: 59 Join Date: 1/2/14 Recent Posts

Is that your custom entity?

thumbnail
Tinh Nguyen, modified 7 Years ago. Junior Member Posts: 31 Join Date: 5/10/18 Recent Posts

Absolutely,  I added successfully to database and show it on searchContainer. But in another jsp file,  i used LocalServiceUtil.getCount then it still return a old result (Here: 10 records).

thumbnail
Olaf Kock, modified 7 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts

I'm reading several conflicting statements here.

The others are right, pointing to Liferay's cache: When you manually insert records into the database, you can't expect Liferay's caches to be current and will need to invalidate them yourself. Or better: Don't write to the database manually.

In case there's something wrong with your code, please share your LocalServiceImpl code for the method that inserts into the database, and the code that calls this in order to insert.

If you're on Liferay 7, you should just use a @Reference to the service (interface) instead of the LocalServiceUtil. (this wouldn't change much in your case, but while we're at it, this could be changed as well)

thumbnail
Tinh Nguyen, modified 7 Years ago. Junior Member Posts: 31 Join Date: 5/10/18 Recent Posts

Actually, I'm trying with a CRUD database example . I'll check my code again then try create a new project.
I'll back when i done.

Anyway thanks for responding.

thumbnail
Tinh Nguyen, modified 7 Years ago. Junior Member Posts: 31 Join Date: 5/10/18 Recent Posts

Finally, i found reason when i using update() of localServiceUtil it will not change cache . I must using add() and everything work perfectly.

I think different thing at model.setNew(true) in add().