Foren

Counter Service auto increasing to "100" after everytime restart server

manh tuan vu, geändert vor 12 Jahren.

Counter Service auto increasing to "100" after everytime restart server

New Member Beiträge: 4 Beitrittsdatum: 06.02.11 Neueste Beiträge
Hi everyone!
I just have problem with CounterLocalServiceUltil that the ids generated was not in sequence, they have the gap of 100 for each time i restart Tomcat Server.
For example: In the Counter table, the currentId of Class Entity A = 100, so that adding a new entity A will have the id of 101. But When i restart server, the currentId of A wil increase to 200, then adding new A will have id of 201.

I want to ids are in squence regardless of restarting server. Do anyone know how to fix that ? please give me advices. Thanks in advance.
thumbnail
Hitoshi Ozawa, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Liferay Legend Beiträge: 7942 Beitrittsdatum: 24.03.10 Neueste Beiträge
Maybe because of the following settings. Setting it to 1 will probably make the counter sequential but decrease the performance.

#
# Set the number of increments between database updates to the Counter
# table. Set this value to a higher number for better performance.
#
counter.increment=100
manh tuan vu, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

New Member Beiträge: 4 Beitrittsdatum: 06.02.11 Neueste Beiträge
Thank Hitoshi for a quick reply, but can you tell me which file i should configure ?
thumbnail
Hitoshi Ozawa, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Liferay Legend Beiträge: 7942 Beitrittsdatum: 24.03.10 Neueste Beiträge
portal-ext.properties file.
If you already haven't created this file to configure your database connection, create it under ROOT\WEB-INF\classes.
thumbnail
Amos Fong, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Liferay Legend Beiträge: 2047 Beitrittsdatum: 07.10.08 Neueste Beiträge
By the way, in addition to Hitoshi's answer, if you are developing your own entities and you want your own counter for it and want to control its own increment, you can use a property similar to how Layouts do it like this:

    #
    # You can further fine tune the counter increment for specific counter
    # names. This entry will ensure that the counter name
    # "com.liferay.portal.model.Layout" or anything that starts with
    # "com.liferay.portal.model.Layout#" will only increment by 1.
    #
    counter.increment.com.liferay.portal.model.Layout=1
Dirk Ulrich, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Junior Member Beiträge: 42 Beitrittsdatum: 21.10.11 Neueste Beiträge
This property seems to set the counter increment fro ALL tables to the desired value.
Is there a way to set the increment value for tables distinctively?
Can I tell Liferay (for each table separately) the step width in which a counter shall be incremented?

For instance, I want just the table 'A' to be incremented by 2 starting with ID 2 so that it only holds even IDs.
Another table 'B' shall be incremented by 2 starting with ID 1 so that it only holds odd IDs.

All other tables shall be incremented by 1.

BTW:
Why does a higher value lead to a better performance???
thumbnail
Hitoshi Ozawa, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Liferay Legend Beiträge: 7942 Beitrittsdatum: 24.03.10 Neueste Beiträge
Did Amos's reply answer your question?

If not, you'll probably have to modify the impl source code for the add method.

It leads to better performance because it leads to fewer database calls.
thumbnail
Gerald Rubin, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Junior Member Beiträge: 59 Beitrittsdatum: 23.10.11 Neueste Beiträge
On a similar note I have this question:

I'm using MySQL 5 with LR 6.1GA1. In service.xml each table has id-type="identity" and the tables created in MySQL have auto-increment set on.

When I, for example, use this code in my Bean:

Person person = PersonLocalServiceUtil.createPerson(0);


it requires an ID as if I had used the counter method to get the next primary key for my table. But I'm not. The database will do the work.

Neither -1 nor 0 as ID value works. What do I pass in to the createEntity method? There's no parameter-less signature.

Thank you.
thumbnail
Gerald Rubin, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Junior Member Beiträge: 59 Beitrittsdatum: 23.10.11 Neueste Beiträge
Replying to my own intrusion in this thread:

If using MySql and auto-increment primary keys are set up in service.xml and, thus, in the database, the correct way to do a createXXX is to pass in 0 as the ID. After you addXXX, you may or may not have the ID field set in your object (50/50 chance, it seems to me).

What I do is to use a DynamicQuery to fetch that entity I just added. This is possible, becuase I've put something unique (other than that ID into it--in most cases the Liferay userId). Then I know the XXX primary key and can use it for further processing.

Maybe all this trouble (really not too much trouble) is because I don't have finders set up and need to handle FKs manually. Only time and experience will teach me if that's the case.
john kammer, geändert vor 10 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

New Member Beiträge: 6 Beitrittsdatum: 01.07.13 Neueste Beiträge
Gerald Rubin:
Replying to my own intrusion in this thread:

If using MySql and auto-increment primary keys are set up in service.xml and, thus, in the database, the correct way to do a createXXX is to pass in 0 as the ID. After you addXXX, you may or may not have the ID field set in your object (50/50 chance, it seems to me).

What I do is to use a DynamicQuery to fetch that entity I just added. This is possible, becuase I've put something unique (other than that ID into it--in most cases the Liferay userId). Then I know the XXX primary key and can use it for further processing.

Maybe all this trouble (really not too much trouble) is because I don't have finders set up and need to handle FKs manually. Only time and experience will teach me if that's the case.


I'm also trying to get the recordID back from the call to addXXXX but in my case am always getting back a recordID of 0 even though in the database the recordID is being incremented correctly. I vaguely recall there being some value in hibernate that would merge the current object with the value in the database and I assume this is not being done (although tracing through the service-builder code to see what is actually going on is a mind-numbing exercise and I have not yet gotten through that particular maze) .

I've run into a couple situations wherein the newly generated recorIDs are conflicting with values already in the database thus causing the new insert to fail. In one case it's clear that I have three different servers all accessing the same database as if they owned it and thus recordID collisions there are not surprising (it's a development environment so we're putting up with it at the moment for other reasons). In our test environment however we just noticed a record ID collision on a system that has only one server generating the recordIDs and that has me confused. Google doesn't seem to show anyone having similar problems but I'm thinking somehow the counter sequence got out of sync. Was hoping to find some method of resetting the counter so that in such instances I could just query the database for the max recordID, increment it and get going again rather than tell the user s/he can't insert the record. Unfortunately I can't find any way to do that - although some of the Liferay javadocs list undocumented methods that sound like might do what I want.

Really wish the Liferay Javadocs were better written (or in some cases written at all).
thumbnail
David H Nebinger, geändert vor 10 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Liferay Legend Beiträge: 14914 Beitrittsdatum: 02.09.06 Neueste Beiträge
john kammer:
I'm also trying to get the recordID back from the call to addXXXX but in my case am always getting back a recordID of 0 even though in the database the recordID is being incremented correctly. I vaguely recall there being some value in hibernate that would merge the current object with the value in the database and I assume this is not being done (although tracing through the service-builder code to see what is actually going on is a mind-numbing exercise and I have not yet gotten through that particular maze) .

I've run into a couple situations wherein the newly generated recorIDs are conflicting with values already in the database thus causing the new insert to fail. In one case it's clear that I have three different servers all accessing the same database as if they owned it and thus recordID collisions there are not surprising (it's a development environment so we're putting up with it at the moment for other reasons). In our test environment however we just noticed a record ID collision on a system that has only one server generating the recordIDs and that has me confused. Google doesn't seem to show anyone having similar problems but I'm thinking somehow the counter sequence got out of sync. Was hoping to find some method of resetting the counter so that in such instances I could just query the database for the max recordID, increment it and get going again rather than tell the user s/he can't insert the record. Unfortunately I can't find any way to do that - although some of the Liferay javadocs list undocumented methods that sound like might do what I want.


First for the 3 systems thing, Liferay always assumes it is the only instance using it's own database (unless you're in a clustering setup). If you have 3 different liferay instances using the DB, then you're definitely doing things wrong.

As for the rest, I'd suggest disabling the entity caching and see if that fixes your issues. Also upgrade to the latest version to ensure you've got all necessary patches. Finally, instead of relying on the counter service to give you ids, use the native auto increment support provided by your database for your entities (yes, this is supported in the service.xml file).

Really wish the Liferay Javadocs were better written (or in some cases written at all).


Join the club, it's got hundreds (thousands?) of members already...
thumbnail
Anita Sahoo, geändert vor 10 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

New Member Beiträge: 13 Beitrittsdatum: 17.09.13 Neueste Beiträge
use counter.increment=1 in portal-ext.properties
That shud work
thumbnail
Madhusudan Aggarwal, geändert vor 8 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

New Member Beiträge: 8 Beitrittsdatum: 24.11.14 Neueste Beiträge
Finally, instead of relying on the counter service to give you ids, use the native auto increment support provided by your database for your entities (yes, this is supported in the service.xml file).


Can you provide any link or example for its sample code of service.xml
As I cannot find any resource on the net.

Another Help I need with:
Batch upload of data directly to the database table.
let's say I add new 500 entries directly into the database table
Since I am using Liferay generated Primary Key, How do I make sure Liferay is aware of these new 500 entries.
So Liferay will add this 500 to its counter value for this entity stored within liferay.

Should I then go and update counter value stored by liferay in counter table for that entity?

Thanks
thumbnail
David H Nebinger, geändert vor 8 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Liferay Legend Beiträge: 14914 Beitrittsdatum: 02.09.06 Neueste Beiträge
If you are using SB, you should use the liferay api for insertions.

Never ever change one of the liferay tables by hand. You've been warned...
Dirk Ulrich, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Junior Member Beiträge: 42 Beitrittsdatum: 21.10.11 Neueste Beiträge
I tried this, Hitoshi. Thank you.

In portal-ext.properties I have this configuration property:
counter.increment.myDomain.myPackage.model.MyClass=2

and in my code I have this statement:
long id = CounterLocalServiceUtil.increment(MyClass.class.getName());


When starting there is NO record in database table 'counter' for MyClass and no record in database table 'my_class'.

The id variable has the value 1 in the first pass and the new entity gets persisted. In the second attempt to persist an object, id has the value 2(!) after calling
long id = CounterLocalServiceUtil.increment(MyClass.class.getName());

while I was excpeting the value 3. Checking the value directly in database table 'counter' for entity MyClass the correct value 3 can be found. Why does
long id = CounterLocalServiceUtil.increment(MyClass.class.getName());
returns a wrong value? Do I have to use another method call?


Can I tell Liferay to start with a specific ID? For instance, table 'A' shall start with 1 while table 'B' shall start with 2.
thumbnail
David H Nebinger, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Liferay Legend Beiträge: 14914 Beitrittsdatum: 02.09.06 Neueste Beiträge
You're missing the actual mechanism here...

When the counter service starts up, it will effectively pre-fetch ids. In your case, by setting the value of 2, it will get 2 IDs as a cached amount. Each call to get a counter value will return 1 greater than the previous call. When the cached IDs are consumed, it will pre-fetch another chunk of IDs to hand out.

So in the normal case, LR pre-fetches 100 IDs to hand out. Each call from the code will only return 1 greater than the previous, but it only hits the database when the 100 IDs it cached are consumed.

IDs that are not consumed during LR's uptime are effectively lost, resulting in the gaps in sequencing that you've seen.

The only way you can guarantee no gaps in the sequencing is to set the increment value to 1 and suffer the database hit every time the counter service is used.
Dirk Ulrich, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Junior Member Beiträge: 42 Beitrittsdatum: 21.10.11 Neueste Beiträge
Thank you, David. A pretty good explanation helping me to understand.
Related to this topic: Is there a way to tell LR to start counting from a certain value on? For instance, the increment shall be 1 but the counter shall start with 1.000.000.
Can I configure this through the portal-ext.properties or do I have to do it on database side by myself, e.g. with an INSERT or UPDATE statement setting counter.currentId to 1000000?
thumbnail
David H Nebinger, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Liferay Legend Beiträge: 14914 Beitrittsdatum: 02.09.06 Neueste Beiträge
I'm not aware of any way to do it, but it would probably be in portal.properties if it is supported. The insert/update into the database manually would get your intended result.
Dirk Ulrich, geändert vor 12 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Junior Member Beiträge: 42 Beitrittsdatum: 21.10.11 Neueste Beiträge
Yes, for the time being I do it using the "database way". emoticon
karthik reddy, geändert vor 9 Jahren.

RE: Counter Service auto increasing to "100" after everytime restart server

Junior Member Beiträge: 60 Beitrittsdatum: 08.04.13 Neueste Beiträge
Dirk Ulrich:
Yes, for the time being I do it using the "database way". emoticon

Hi,
I have issue can some one give me the solution for this
I need to give counter.increment which we are using in portal-ext.properties but I need for specific portlet(instead of portal level)