3

I am working on a project that uses REST service(Jersey implementation) for some features like login,register etc.

At the login time i want to store the current user details in DB and that is successfully done with the below piece of code:-

Userlogin userlogin = new Userlogin(user,loginDateTime,null,ipAddress,tokenEnc,salt);
userloginBean.persist(userlogin);
userLoginId = userlogin.getId();
logger.log(Level.INFO, "userLoginId: "+userLoginId);

Now look at the third line of the code, i am trying to get the recently inserted id from the userlogin object. But it giving me null. In 4th line when i try to print it in log it show null.

I put some logs to check the execution and i found that the actual mysql query is executing after the 4th line.

So please tell me how can i sure that mysql has completed its query?

Also Explain me if possible ,why it is behaving like this as i use the same type code and i never get such situation.

I am using eclipse Kepler, Jsf 2.2
My project is Dynamic web project not maven.
For mysql query execution i am using Criteria API in DAO class. I am persisting database using JPA.

Here is the persist method of my UserloginBean.java class:

public void persist(Userlogin transientInstance) {
    log.log(Level.INFO, "persisting Userlogin instance");
    try {
        entityManager.persist(transientInstance);
        log.log(Level.INFO, "persist successful");
    } catch (RuntimeException re) {
        log.log(Level.INFO, "persist failed", re);
        throw re;
    }
}

Here is some piece of my eclipse log:

2014-05-27T00:26:17.737+0530|INFO: persisting Userlogin instance
2014-05-27T00:26:17.813+0530|INFO: persist successful
2014-05-27T00:26:17.814+0530|INFO: userLoginId: null
2014-05-27T00:26:17.830+0530|FINE: INSERT INTO userlogin (help, ipaddress, 
login, logout, token, user_id) VALUES (?, ?, ?, ?, ?, ?)
    bind => [6 parameters bound]
2014-05-27T00:26:17.842+0530|FINE: SELECT LAST_INSERT_ID()

Thanks in advance. Sorry for my english mistake if i did any.

Vikas Sharma
  • 579
  • 1
  • 5
  • 15
  • 2
    unless you're running the query in a separate thread, they should block the current thread until the query's completed and results are available. – Marc B May 26 '14 at 19:23
  • i also thought the same but unfortunately this is not happening..i set the eclipse log level to FINE so that i can easily see that INSERT query printed in log long time after my 4th line of code.. – Vikas Sharma May 26 '14 at 19:26
  • What type of persistence do you use? If you are using JPA, the insert might be delayed until you commit, explaining the observerd behaviour. – ruediste May 26 '14 at 19:34
  • I am using JPA for persistense, i updated my question and try to put some necessary information, thanks @ruediste – Vikas Sharma May 26 '14 at 19:40

1 Answers1

1

If you're using JPA, you'll need to call flush on the entityManager to see the values of fields that come from the database. e.g. generated id's, columns with default values, etc...

BillRobertson42
  • 12,602
  • 4
  • 40
  • 57