users@glassfish.java.net

Re: How to simulate JPA's "strategy=TABLE" PK generation using JDBC?

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Mon, 08 Oct 2007 18:01:01 -0700

Witold,

Won't you need to use that table for the batch inserts? How otherwise can you
guarantee the unique ids?

I would expect that select for update should work correctly and a parallel
transaction would just wait for your batch operation to finish because it's the
database that ensures the correct procedures.

Regards,
-marina

Witold Szczerba wrote:
> Hi there,
> in my application, I use Toplink Essentials' JPA implementation with
> Glassfis v2 server.
> Application is almost finished, but I encountered problem with batch
> inserting records into database - TopLink is not good with that
> because it does not implement em.getReferece(...), it does exactly the
> same thing what em.find(...).
>
> I have to insert large amount of Transaction entities into database -
> 20000 or more and each transaction requires Loan entity. If I would
> invoke em.find(...) for each Loan - the operation would last for ever
> and it would eat entire memory, so I have to do it using plain JDBC
> (stmt.addBatch() and stmt.executeBatch()).
>
> The problem is with ID generation - my application uses default
> TopLink strategy - there is a SEQUENCE table with SEQ_NAME and
> SEQ_COUNT columns.
>
> How am I suppose to use it not to get in conflict with what TopLink
> does? This application is used by a large amount of users in the same
> time and I do not want to brake anything by incorrect use of SEQUENCE
> table.
>
> I was thinking: maybe I should, just before inserting those 20000
> entities, read the SEQ_COUNT column, remember the value and update it
> with its old value + the number of entities I want to add - all this
> in a separate transaction just before inserting... But what If my
> transaction would fail? I would have to throw into the trash all 20000
> PKs (they would be useless)... So, maybe I can SELECT FOR UPDATE that
> SEQUENCE table and increment it in the same transaction with my
> entities insertion, but what if in the mean time TopLink would run out
> of available PKs and would like to fetch more? Would TopLink just wait
> for my transaction to finish or every JPA insert into database would
> cause exception?
>
> What are your suggestions?
>
> Best regards,
> Witold Szczerba
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>