Fórum

liferay-ce-portal-7.0-ga3- Assign primary key to SB instead of increment

Theja Babu, modificado 6 Anos atrás.

liferay-ce-portal-7.0-ga3- Assign primary key to SB instead of increment

New Member Postagens: 15 Data de Entrada: 02/06/17 Postagens Recentes
Hi All,

Is there a why that i can assign the primary and save the record with out using "increment" .

My requirement is external other service builder will also insert the data to the same table and i will try to insert the records to the same table with my own service builder class. Since other SB inserted records and my SB will have old PK as reference and when i try to insert it say duplicate key found and fails to insert.
Scenario like below:
My service builder insert 1-5 and Other SB insert 6-10 using insert statement and now i try to insert it was going as 6,7...and fails
Can i pass 11 instead of using increment as id-type and call update method in SB?
thumbnail
Djamel TORCHE, modificado 6 Anos atrás.

RE: liferay-ce-portal-7.0-ga3- Assign primary key to SB instead of incremen

New Member Postagens: 18 Data de Entrada: 21/10/14 Postagens Recentes
Hi,
Are you using counterLocalService for new insert operations ?
If not, try to use it like :

YourClassEntity newEntity =  yourClassEntityPersistence.create(counterLocalService.increment(YourClassEntity .class.getName()));



then, copy the data to the entity, then call update :


yourClassEntityPersistence.update(newEntity );


If the above solution does not work for you, try the solution below (in portal-ext.properties file and restart the server) but it is not recommended for performance reasons specially if you do bulk inserts:

#
# Set the number of increments between database updates to the Counter table. 
# Set this value to a higher number for better performance.
#
counter.increment=1


PS: Liferay database tables (included tables generated by Service Builder) must be managed only by one Liferay instance in Write Mode to ensure data reliability and respect the model constraints.

Kind regards,
Djamel
Theja Babu, modificado 6 Anos atrás.

RE: liferay-ce-portal-7.0-ga3- Assign primary key to SB instead of incremen

New Member Postagens: 15 Data de Entrada: 02/06/17 Postagens Recentes
Thanks Djamel.

As you mention I'm using the Counter service like below and facing the same issue.

long eventPmKey = CounterLocalServiceUtil.increment(Events.class.getName());
Events event = eventsLocalService.createEvents(eventPmKey);

My understanding is the table is accessed by two different SB each one has its own PK generated and try to insert. As SB1 insert records from 1-5 and SB2 try to insert the same no again and getting a duplicate key error. Is that something that possible to assign the primary key manually query the table, get the last inserted record and increment it by one and save that as a new record?
thumbnail
Olaf Kock, modificado 6 Anos atrás.

RE: liferay-ce-portal-7.0-ga3- Assign primary key to SB instead of incremen

Liferay Legend Postagens: 6403 Data de Entrada: 23/09/08 Postagens Recentes
Theja Babu:
My understanding is the table is accessed by two different SB each one has its own PK generated and try to insert. As SB1 insert records from 1-5 and SB2 try to insert the same no again and getting a duplicate key error. Is that something that possible to assign the primary key manually query the table, get the last inserted record and increment it by one and save that as a new record?


As 7.0 CE does not support clustering, you can't have two instances update the same table, or you'll run into exactly this kind of issues: Using CounterLocalService would be sufficient, if the two counterLocalServices would coordinate with each other (which they do in a cluster). As they don't, I'd be expecting the same kind of problems with all kinds of object creations all over, not just with your custom entity.

Any way: You'll need to make sure to have unique primary keys. If you're using permission checks (e.g. if you include Resource objects), they must also be unique within the resource context. This means you're best off using Liferay mechanisms. If you only need your own entity and no Liferay frameworks (resources, assetEntries etc), then you're free to come up with any mechanism that suits you for generating your primary keys.

I'm still wondering how you have managed to setup two SBs writing to the same database, without failing a lot earlier...
Theja Babu, modificado 6 Anos atrás.

RE: liferay-ce-portal-7.0-ga3- Assign primary key to SB instead of incremen

New Member Postagens: 15 Data de Entrada: 02/06/17 Postagens Recentes
Thanks Olaf Kock,

In my case there are multiple teams involved in the development and each one has their own SB's. Is there a way that I can solve this problem when a table is inserting from two different SB's
thumbnail
Olaf Kock, modificado 6 Anos atrás.

RE: liferay-ce-portal-7.0-ga3- Assign primary key to SB instead of incremen

Liferay Legend Postagens: 6403 Data de Entrada: 23/09/08 Postagens Recentes
Theja Babu:
In my case there are multiple teams involved in the development and each one has their own SB's. Is there a way that I can solve this problem when a table is inserting from two different SB's


If you use the code you've quoted above, this all should be ok:

long eventPmKey = CounterLocalServiceUtil.increment(Events.class.getName());
Events event = eventsLocalService.createEvents(eventPmKey);


However, if you manually increment this for inserting the next event, it will certainly fail. You'll always have to acquire a new primary key from CounterLocalService and must not assume that you can increment it yourself. E.g.

long eventPmKey = CounterLocalServiceUtil.increment(Events.class.getName());
Events event = eventsLocalService.createEvents(eventPmKey);
Events moreEvents = eventsLocalService.createEvents(++eventPmKey); // WRONG


will be problematic. eventPmKey is a primary key and you shouldn't do any mathematic operation on it apart from comparing it to other primary keys.