H2文件模式,在现有文件db上打开新连接时出错

时间:2013-04-26 15:11:05

标签: java h2

我正在尝试为文件创建H2数据库。 首先运行一个小模块,创建文件和模式,并用数据填充它。 在执行此操作时,使用了H2 jdbc url:

jdbc:h2:file:c:\\Yogesh\\TestH2Db;MODE=Oracle;INIT=CREATE SCHEMA IF NOT EXISTS TEST\\;SET   SCHEMA TEST\\;

运行架构的DDL脚本然后在此我使用RunScript类加载一些数据。 加载完成后,此模块停止(JVM死亡/不再运行)。

第二个模块,尝试使用此预先创建的数据来运行一些测试。 要连接到此数据库,我正在尝试使用此URL:

jdbc:h2:file:c:\\Yogesh\\TestH2Db;IFEXISTS=TRUE;MODE=Oracle;INIT=CREATE SCHEMA IF NOT EXISTS TEST\\;SET   SCHEMA TEST\\;

基本上是相同的网址,但添加了IFEXISTS = TRUE。 基于此URL创建JdbcConnectionPool。

protected JdbcConnectionPool initConnectionPool(String user, String pwd,
        int maxConn, String jdbcUrl) {
    System.setProperty("h2_jdbc_url", jdbcUrl);
    JdbcConnectionPool connpool = JdbcConnectionPool.create(
            jdbcUrl, user, pwd);
    connpool.setMaxConnections(maxConn);
    return connpool;

当模块的其余部分在上面创建的JDBC连接池上执行getConnection时,它会失败并且似乎创建了我在Init模块中再次创建的相同对象。

我试过没有INIT = CREATE SCHEMA IF NOT NOT EXISTS TEST \; SET SCHEMA TEST \;无济于事

编辑:添加堆栈跟踪

org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement 
[Have a create view statement here that fails cant give out the SQL statement, 
however note that this SQL statement ran fine 
when i used RunScript tool to init the DB schema; 
What i don't understand is while doing get connection the 
connection Pool why are objects 
being inited again, the Schema creation script is not part of the 
INIT part of JDBC Url]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
    at org.h2.message.DbException.get(DbException.java:169)
    at org.h2.message.DbException.getSyntaxError(DbException.java:194)
    at org.h2.command.Parser.getSyntaxError(Parser.java:491)
    at org.h2.command.Parser.read(Parser.java:2798)
    at org.h2.command.Parser.readIfMore(Parser.java:809)
    at org.h2.command.Parser.parseColumnList(Parser.java:778)
    at org.h2.command.Parser.parseCreateView(Parser.java:4281)
    at org.h2.command.Parser.parseCreate(Parser.java:3749)
    at org.h2.command.Parser.parsePrepared(Parser.java:324)
    at org.h2.command.Parser.parse(Parser.java:279)
    at org.h2.command.Parser.parse(Parser.java:255)
    at org.h2.command.Parser.prepare(Parser.java:201)
    at org.h2.engine.Session.prepare(Session.java:388)
    at org.h2.engine.Session.prepare(Session.java:375)
    at org.h2.engine.MetaRecord.execute(MetaRecord.java:56)
    at org.h2.engine.Database.open(Database.java:632)
    at org.h2.engine.Database.openDatabase(Database.java:222)
    at org.h2.engine.Database.<init>(Database.java:217)
    at org.h2.engine.Engine.openSession(Engine.java:56)
    at org.h2.engine.Engine.openSession(Engine.java:159)
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:138)
    at org.h2.engine.Engine.createSession(Engine.java:121)
    at org.h2.engine.Engine.createSession(Engine.java:28)
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:305)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:109)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:93)
    at org.h2.Driver.connect(Driver.java:72)
    at org.h2.jdbcx.JdbcDataSource.getJdbcConnection(JdbcDataSource.java:181)
    at org.h2.jdbcx.JdbcDataSource.getXAConnection(JdbcDataSource.java:315)
    at org.h2.jdbcx.JdbcDataSource.getPooledConnection(JdbcDataSource.java:341)
    at org.h2.jdbcx.JdbcConnectionPool.getConnectionNow(JdbcConnectionPool.java:226)
    at org.h2.jdbcx.JdbcConnectionPool.getConnection(JdbcConnectionPool.java:199)
    at junit.db.utils.InitH2DB.getConnection(InitH2DB.java:45)
    at test.DBLoader.DBLoader.main(DBLoader.java:21)

1 个答案:

答案 0 :(得分:1)

错误是因为用于创建H2模式的脚本不正确。 有关详细信息,请参阅问题下方的评论。