jeudi 31 août 2017

Java Reflection Issure resulting in Read Only Transaction Error

I have two data sources (persistent contexts) one is Read Only, and one is for Read-Write, everyone pointing to a different database.

Read-Only is configured to be outside of JTA, as I going to only read from it.

So in this way, we're migrating all related to reads/fetch/gets from database to retrieve data from the read only DB.

Take a look at this code:

ActionItem item = pEm.find( ActionItem.class, pActionItemViewModel.getId() );
item.setActionTimestamp( new LocalDateTime( pActionItemViewModel.getActionDate() ) );
ActionItem itemTemp = pEm.merge( item );

pEm is the entity manager, as you know the find searches for the ActionItem, then it updates with the LocalDateTime, then it use the 'merge' function...

Problem is, in pEm.find inside of the function (that i've overrided) i use the read only entity manager, so it fetch from the read DB, problem is with the merge, because it's giving me an ERROR saying:

Caused by: org.postgresql.util.PSQLException: ERROR: cannot execute UPDATE in a read-only transaction

And it's because inside of the merge, i use reflection to get the same object from the read-write entity manager, to enabled transaction to commit in the read-write, but for some reason it's not working, this is the merge function:

public <T> T merge(T t) {
    try {
        return (T) this.readWriteEntityManager.merge(this.readWriteEntityManager.getReference(t.getClass(), t.getClass().getMethod("getId").invoke(t)));
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
        return null;
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        return null;
    } catch (InvocationTargetException e) {
        e.printStackTrace();
        return null;
    }
    // return this.readWriteEntityManager.merge(t);
}

Thing is, if i perform the get reference outside the merge, it works, like this...

ActionItem itemT = pEm.find( ActionItem.class, pActionItemViewModel.getId() );
ActionItem item = pEm.getReference(itemT.getClass(),itemT.getId());
item.setActionTimestamp( new LocalDateTime( pActionItemViewModel.getActionDate() ) );
ActionItem itemTemp = pEm.merge( item );

Any idea would be appreciated... (help help help)





Aucun commentaire:

Enregistrer un commentaire