0

I am in a configuration (JTA - CMT) in which I have to remove an entity object after creating it. I use em.remove(object) after finding it, but remove doesn't work, or it seems to be not working.

What happens is that the object I want and try, to remove is still in DB.

Once the removal operation is performed, if I later try to retrieve the records from the entity table it belongs to, I GET THIS OBJECT (obviously with the other objects I previously saved). My check into the entity table, accessing by a shell the DB, confirms that objects I had to remove are stored in the table. It's strange. But if I try to do a little test - inside the method that calls remove() - of retrieving the object, right after calling remove() on it, by a new call to find(), the system, rightly, returns null (it says me that that object does not exist).

MyEntity object = em.find(MyEntity.class, my_entity_id);  // it is found
if (object != null) {
    em.remove(object);
    log.debug("something here");
} else
    log.debug("something else here");

// here i am testing whether it is still existing
object = em.find(MyEntity.class, my_entity_id);  // it is not found, returns null

I try also with using flush() right after remove() but nothing changes, actually an error emerges:

"java.sql.SQLException: IJ031070: Transaction cannot proceed: STATUS_MARKED_ROLLBACK"

Is there anyone that has encountered this situation? Any suggestion?

tcoddis
  • 21
  • 5
  • are you surrounding that code with a transaction which is not read only? – Maciej Kowalski Feb 20 '19 at 12:03
  • the code i put is the proper management of transactions in a JTA-CMT configuration (with that IF statement). I don't know what to do to set transaction management features, i am new to JPA – tcoddis Feb 20 '19 at 12:07
  • so you have @Transactional annotated method i assume? – Maciej Kowalski Feb 20 '19 at 13:30
  • indeed no. I don't need it. The configuration JTA+CMT makes everything automatical, it seems. – tcoddis Feb 20 '19 at 13:44
  • is remove / persist / merge working in other places? – Maciej Kowalski Feb 20 '19 at 14:00
  • I am not using merge(). I have a method (A) in which i create an object and i persist it. I have another method (B) in which i use remove(). This method takes as input something representing an entity id. I have a third method in which i firstly call A (i create and persist an object); then, if something goes wrong, i have to call B (i have to remove the object persisted in A). – tcoddis Feb 20 '19 at 14:20
  • wait, what do you mean for "other places"? i don't have those operations just in the methods i have mentioned.. example, at the point in which "something may go wrong", i have a creation of an entity object. This entity (let's name it "E") has a reference to the entity object i could need to remove. So, in my third method i call A, then i create an object E passing to it the object created and persisted in A. This latter creation may go wrong: so i cannot persist the E object and, important, i have, at this point, to remove, via B, that MyEntity object created and persisted in A. – tcoddis Feb 20 '19 at 14:36

0 Answers0