I am using java application with google juice as a injector along with jdbi as database layer.I need to fetch sequence from database using java code.
I have tables in database for the application,which sequence are not used.But for other purpose I need sequence value as current ,next and last.
although the following queries are working in pg-admin
select currval('testsequence');
select nextval('testsequence');
select last_value FROM testsequence;
the code is as follows.
public long getCurrentSequenceFromDB()
{
LOGGER.info("SequenceHelper :getCurrentSequenceFromDBSTART");
try (Handle handle = jdbi.open())
{
SequenceDAO squenceDAO = handle.attach(SequenceDAO.class);
long sequence = squenceDAO.getCurrentSequence();
if (sequence != 0)
{
LOGGER.info("SequenceHelper :getCurrentSequenceFromDBEND");
return sequence;
}
else
{
LOGGER.info("SequenceHelper :getCurrentSequenceFromDBsequence sequence is null");
return 0;
}
}
catch (Exception e)
{
LOGGER.error("Error in getting sequence from database", e);
throw new SignageServiceException(ErrorCodes.UNEXPECTED_ERROR, "Error in getting sequence from database", e);
}
}
and at query level as:
@SqlQuery("select currval('public.testsequence')")
public long getCurrentSequence();
I get error as
Caused by: org.postgresql.util.PSQLException:
ERROR: currval of sequence "testsequence" is not yet defined in this session