[resolved] Problem with Inserting/Updating in DescriptorEventListener callback methods [message #1096334] |
Wed, 28 August 2013 08:47 |
Tony Oganesian Messages: 9 Registered: July 2009 |
Junior Member |
|
|
Hi,
Would greatly appreciate a pointer in the right direction with the following issue:
- I am registering a DescriptorEvent listener and receiving a preUpdate() callback when a UOW.commit() executes and my main object is saved
- In the preUpdate() callback I would like to perform an insert, update or delete operations for some unrelated objects (mapped with EL, but not related to the main object) and I would like these inserts/updates to happen in the same transaction as the main save
I tried the following 3 methods without success. What's the proper way of doing this?
Following code is simplified for readability. Actual code sets the relevant properties of the LogRecord based on ev.getSource() object.
public void preUpdate(DescriptorEvent ev)
{
UnitOfWork nestedUOW = ev.getSession().acquireUnitOfWork();
LogRecord lr = (LogRecord) nestedUnitOfWork.registerNewObject(new LogRecord());
lr.setTimeStamp(new Date());
nestedUOW.commit();
}
No exception, but no insert happens
--------------------------
public void preUpdate(DescriptorEvent ev)
{
UnitOfWork parentUOW = (UnitOfWork) ev.getSession();
LogRecord lr = (LogRecord) parentUOW.registerNewObject(new LogRecord());
lr.setTimeStamp(new Date());
}
No exception, but no insert happens
--------------------------
public void preUpdate(DescriptorEvent ev)
{
LogRecord lr = new LogRecord();
lr.setTimeStamp(new Date());
InsertObjectQuery insertObjectQuery = new InsertObjectQuery(lr);
ev.getSession().executeQuery(insertObjectQuery);
}
Exception telling me that I shouldn't be issuing direct queries via UOW
Exception Description: Objects cannot be written during a UnitOfWork, they must be registered.
[Updated on: Thu, 29 August 2013 04:38] Report message to a moderator
|
|
|
|
Re: Problem with Inserting/Updating in DescriptorEventListener callback methods [message #1096687 is a reply to message #1096648] |
Wed, 28 August 2013 18:26 |
Tony Oganesian Messages: 9 Registered: July 2009 |
Junior Member |
|
|
James,
Thanks a lot for looking into this!
Third method indeed is what some samples that I found suggest I use, but it does not work, since the ev.getSession() is a UOW and it throws (Exception Description: Objects cannot be written during a UnitOfWork, they must be registered.) As far as I see it my options are limited to the following:
- I can't use UOW registration mechanisms because at this point it's too late, EL is already in the process of calculating changes and my newly registered objects are ignored.
- Issuing direct queries on the current UOW session would be ideal, I just need help figuring out how to bypass UOW validation that prevents it from happening
- Obtaining a new independent session/UOW. This works, but I am a little uncomfortable since it commits LogRecords in a different transaction, so I may end-up with the LogRecord even if the main transaction rolls-back
I am using the preUpdate(), because sometimes none of the EL-tracked attributes in the main object are changed, but I still need to create a LogRecord
--
Tony
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03182 seconds