Question

I have a method to save a new object in an EJB bean. This method is called, without error, but nothing changes in the database. I can't understand why.

Here is the code:

@Stateless(name = "Ar", mappedName = "ManagementBean")
public class ManagementBean implements IManagementBeanLocal, IManagementBeanRemote {
...
    @Override
    public int storeRawSms(String raw, String requestUid, String text, String service, boolean correctlyAnalysed, Date receivedTimestamp,
            boolean toBeAnalysed, String phoneNumber) {

        // Get phone number, create if it dosn't exist
        PhoneNumber pn = getOrCreatePhoneNumberPrivate(phoneNumber);

        // Create rawSMS
        RawSms rawSms = new RawSms(raw, requestUid, text, service, correctlyAnalysed, receivedTimestamp, toBeAnalysed, pn);

        // Store and return result
        em.persist(rawSms);
        int result =  rawSms.getId();

        em.flush();
        em.clear();

        return result;
    }

...

And the caller:

@PersistenceContext private EntityManager em; 
... 
int rawSmsIs = bean.storeRawSms(raw, requestUid, message, service, false, new Date(), true, sender);

Do you have an idea?

Was it helpful?

Solution 3

It seems that your transaction never commited, so try changing transaction management:

@Stateless(name = "Ar", mappedName = "ManagementBean")
@TransactionManagement(TransactionManagementType.BEAN)
public class ManagementBean implements IManagementBeanLocal, IManagementBeanRemote {

      @Resource
      private UserTransaction utx;

      @Override
      public int storeRawSms(..) {

            try {
                  utx.begin();
                  ..
                  em.persist(rawSms);
                  int result =  rawSms.getId();
                  utx.commit();
            }
            catch(Exception ex) {
                  //EXCEPTION HANDLING
                  utx.rollback();
            }
      }
}

OTHER TIPS

I see that you inject a reference to the EntityManager in the client (not sure why), but I don't see it in the session bean (maybe simply because you did not include the line in your message). Is it possible that you forgot to use the annotation @PersistenceContext in your stateless session bean?

Also, be careful: depending on the JPA implementation you are using and the generation strategy for the ids, you should call flush() before calling getId(). Indeed, if you let the DB generate your IDs, then you need a flush() to have this happen before the method returns the value.

Thanks, the prposed solution worked!

I use the container-managed transactions like this:

@Stateless(name = "Ar", mappedName = "ManagementBean")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ManagementBean implements IManagementBeanLocal, IManagementBeanRemote {
....
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public int storeRawSms(String raw, String requestUid, String text, String service, boolean correctlyAnalysed, Date receivedTimestamp, boolean toBeAnalysed, String phoneNumber) {
....

Thanks again!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top