users@glassfish.java.net

Transaction control with EJB for FOR UPDATE

From: <glassfish_at_javadesktop.org>
Date: Thu, 17 May 2007 12:29:08 PDT

Hi, i'm trying to select a record from a table with "FOR UPDATE" using glassfish 9.0_01 b14 so that i can effectively lock it while i process the action, but i've encountered some problems with regards transactions.

I've got the ejb function surrounded by a

[b]@TransactionAttribute(value=TransactionAttributeType.REQUIRES_NEW)[/b]

attribute to force a new transaction to be created, but it seems to have no effect in certain cases.

For example if i create a native named query using the entity class as the result like so
[b]@NamedNativeQuery("query", "SELECT xxxx FROM xxx WHERE xxx FOR UPDATE", resultClass=entity.class);[/b]

then the transaction isn't started before the query executes. In the log with JPA logging set to FINEST i get

[b]TX_bind
execute_query
reconnecting_to_external_connection_pool
SELECT xxx FROM xxx WHERE xxx FOR UPDATE bind => [1179429077048]
execute_query
TX_begin
UPDATE xxx
[/b]

You can see the transaction is started before i update the entity which is too late.

However if i change the result to be a mapping for example and select a single/multiple columns
[b]
@SqlResultSetMapping(name="SellerID",
        columns={_at_ColumnResult(name="seller_id")}
)
@NamedNativeQuery("query", "SELECT xxxx FROM xxx WHERE xxx FOR UPDATE", resultSetMapping="SellerID");[/b]

then i get
[b]
execute_query
TX_begin
reconnecting_to_external_connection_pool
SELECT xxx FROM xxx WHERE xxx FOR UPDATE bind => [1179429077048]
execute_query
UPDATE xxx
[/b]


I've even tried using the query hint [b]toplink.refresh[/b] and setting it to true in the first case but this has no effect

While i can select the primary key of the entity using the above case, i have to make another select to fetch the rest of the entity. Also if i select all the columns, i can only create a detached entity which again has to be merged back in manually

I've also tried to create a native query but this behaves as the first case abiove...

Does any one know if this is a bug or an expected feature? Has any one got any ideas to get around it?

Thanks
[Message sent by forum member 'jsl123' (jsl123)]

http://forums.java.net/jive/thread.jspa?messageID=217707