I have the following function which calls nextval on an Oracle sequence, but i noticed a strange behavior. The function is :
public class DAOImplementation{
private static SessionFactory factory;
private Session session;
public BigDecimal getSequenceNextVal(){
BigDecimal result = null;
factory = HibernateUtil.getSessionFactory();
session = factory.openSession();
String sql = "select sequence_test.nextval from dual";
Query query = session.createSQLQuery(sql);
result = (BigDecimal) query.uniqueResult();
session.close();
return result;
}
}
When I print in console the value returned by the above function, I can see the correct next value. But, when I run on DB "select sequence_test.currval from dual" the value is not corresponding to the one in the console. If I reconnect to the Oracle DB from PL/SQL Developer and run the "select sequence_test.nextval from dual", the result is incremented by 2(i guess once by the java function and once more by the query executed manually).
Can you please help me to understand this situation?