public static void main(String[] args) throws Exception {
String jdbcUrl =
"jdbc:h2:mem:;MODE=PostgreSQL;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=4";
String driver = "org.h2.Driver";
Class.forName(driver);
Connection conn = DriverManager.getConnection(jdbcUrl, "DBA", "");
Statement st = conn.createStatement();
st.executeUpdate("drop schema conf if exists; create schema conf;");
st.executeUpdate("create sequence conf.report_id_seq;");
st.executeQuery("select nextval('conf.report_id_seq');");
}
Exception in thread "main" org.h2.jdbc.JdbcSQLException: Sequence
CONF.REPORT_ID_SEQ not found; SQL statement:
select nextval('conf.report_id_seq'); [90036-101]
at org.h2.message.Message.getSQLException(Message.java:103)
at org.h2.message.Message.getSQLException(Message.java:114)
at org.h2.message.Message.getSQLException(Message.java:77)
at org.h2.schema.Schema.getSequence(Schema.java:423)
at org.h2.expression.Function.getSequence(Function.java:1125)
at org.h2.expression.Function.getValueWithArgs(Function.java:1025)
at org.h2.expression.Function.getValue(Function.java:406)
at org.h2.command.dml.Select.queryFlat(Select.java:499)
at org.h2.command.dml.Select.queryWithoutCache(Select.java:560)
at org.h2.command.dml.Query.query(Query.java:233)
at org.h2.command.CommandContainer.query(CommandContainer.java:81)
at org.h2.command.Command.executeQueryLocal(Command.java:140)
at org.h2.command.Command.executeQuery(Command.java:121)
at org.h2.jdbc.JdbcStatement.executeQuery(JdbcStatement.java:75)
If I simply remove the ".conf" from the create/select, it works fine.
Thanks.
This is an incompatibility with PostgreSQL. I will fix that in the
next release. A workaround is to use:
select nextval('CONF', 'REPORT_ID_SEQ');
Unfortunately this is not supported by PostgreSQL.
Regards,
Thomas
I work out of svn, so I'll keep an eye out for that fix.
Thanks!
> That new syntax does the trick, thanks. I notice that the case seems
> to matter.
Yes, this syntax is case sensitive. The regular nextval('...') is not
however (and will not be).
> The case
> does not seem to matter in "create sequence" or "create schema".
That's because the database converts unquoted names to uppercase. See also:
http://www.h2database.com/html/grammar.html#name
http://www.h2database.com/html/grammar.html#quotedname
> I confess ignorance as to how this should actually work in standard SQL,
> but it is a surprise.
As far as I know, H2 works like defined by the SQL standard. Other
databases work differently.
> I work out of svn, so I'll keep an eye out for that fix.
It is now in the SVN.
Regards,
Thomas
Thanks for the fix. There seems to be a similar issue with the
following syntax:
select last_value from conf.report_id_seq
This is also likely a PostgreSQL-specific syntax.
org.springframework.jdbc.UncategorizedSQLException: Hibernate
operation: could not execute query; uncategorized SQLException for SQL
[select last_value from conf.report_id_seq]; SQL state [90079]; error
code [90079]; Schema CONF not found; SQL statement:
select last_value from conf.report_id_seq [90079-102]; nested
exception is org.h2.jdbc.JdbcSQLException: Schema CONF not found; SQL
statement:
select last_value from conf.report_id_seq [90079-102]
Caused by: org.h2.jdbc.JdbcSQLException: Schema CONF not found; SQL statement:
select last_value from conf.report_id_seq [90079-102]
at org.h2.message.Message.getSQLException(Message.java:103)
at org.h2.message.Message.getSQLException(Message.java:114)
at org.h2.message.Message.getSQLException(Message.java:77)
at org.h2.command.Parser.getSchema(Parser.java:596)
at org.h2.command.Parser.readTableOrView(Parser.java:4203)
at org.h2.command.Parser.readTableFilter(Parser.java:967)
at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1496)
at org.h2.command.Parser.parseSelectSimple(Parser.java:1593)
at org.h2.command.Parser.parseSelectSub(Parser.java:1490)
at org.h2.command.Parser.parseSelectUnion(Parser.java:1335)
at org.h2.command.Parser.parseSelect(Parser.java:1323)
at org.h2.command.Parser.parsePrepared(Parser.java:392)
at org.h2.command.Parser.parse(Parser.java:288)
at org.h2.command.Parser.parse(Parser.java:260)
at org.h2.command.Parser.prepareCommand(Parser.java:232)
at org.h2.engine.Session.prepareLocal(Session.java:352)
at org.h2.engine.Session.prepareCommand(Session.java:313)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1042)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:72)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:219)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:497)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:415)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1538)
at org.hibernate.loader.Loader.doQuery(Loader.java:661)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2211)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
at org.hibernate.loader.Loader.list(Loader.java:2090)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:811)
at com.example.ips.report.data.dao.hibernate.conf.ReportConfHibernate$8.doInHibernate(ReportConfHibernate.java:239)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:338)
at com.example.ips.report.data.dao.hibernate.conf.ReportConfHibernate.getLastReportId(ReportConfHibernate.java:233)
at com.example.ips.report.data.dao.conf.ReportConfDaoTest.getLastReportId(ReportConfDaoTest.java:74)
> Schema ADMIN not found
It looks like the schema and the sequence was not created. In that
case it's not possible to get the next value. I don't think this is a
bug in H2. Do you have a reproducible test case?
Regards,
Thomas
This was my mistake. Sorry to have wasted your time.
Thanks,
L