我正在使用Java应用程序,其中Google Juice作为注入器,JDBI作为数据库层。我需要使用Java代码从数据库中提取序列。
我的应用程序在数据库中有一些表,其中序列没有被使用。但是,出于其他目的,我需要获得当前、下一个和最后一个序列值。
尽管以下查询在pg-admin中有效,
select currval('testsequence');
select nextval('testsequence');
select last_value FROM testsequence;
代码如下。
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);
}
}
在查询级别上:
@SqlQuery("select currval('public.testsequence')")
public long getCurrentSequence();
我遇到错误,信息如下:
Caused by: org.postgresql.util.PSQLException:
ERROR: currval of sequence "testsequence" is not yet defined in this session