Search code examples
jpatransactionsejbcommitoracle-adf

Committing changes to database


I have an EJB based project and I want to commit the values in the input fields to the database. I dragged the merge method from the data controls and dropped it as a button but there is no effect on clicking on it. I cannot find where is the fault, because I am a beginner, so I will be very grateful if you help me or give me some piece of advice!

Action of the button:

<af:button actionListener="#{bindings.mergeEntity.execute}" text="mergeEntity" disabled="#{!bindings.mergeEntity.enabled}" id="b1"/>

Code from session bean:

private void commitTransaction() 
{ 
    final EntityTransaction entityTransaction = em.getTransaction(); 
    if (!entityTransaction.isActive()) 
    { 
        entityTransaction.begin(); 
    } 
    entityTransaction.commit(); 
}


public <T> T mergeEntity(T entity) 
{ 
    entity = em.merge(entity); 
    commitTransaction(); 
    return entity; 
} 

Solution

  • I tried to piece together the code fragments you posted in the comments, and I see something wrong with your code.

    public <T> T mergeEntity(T entity) { 
        entity = em.merge(entity); 
        commitTransaction(); 
        return entity; 
    }
    
    private void commitTransaction() { 
        final EntityTransaction entityTransaction = em.getTransaction(); 
        if (!entityTransaction.isActive()) { entityTransaction.begin(); 
        } entityTransaction.commit(); 
    } 
    

    You only called commitTransaction method after the merge operation. Inside your commitTransition is the only time when try you start/begin your transaction. That means, your merge operation is executed outside the boundaries of your transaction. This is wrong.

    It should have been:

    entityTransaction.begin(); 
    entity = em.merge(entity); 
    entityTransaction.commit();