我可以通过 Eclipse Database Development 连接 Derby,但无法通过 Hibernate 将具有相同 url 的 Derby 连接到 Eclipse Database Development。
错误:
"Caused by: java.sql.SQLException: Another instance of Derby may have already booted the database "
最佳答案
几天前刚刚完成了一个项目,使用 Derby 和 Hibernate 来完成此任务。 (Derby 在同一个 JVM 中运行。)
据我了解,当您使用嵌入式驱动程序时,它默认启动数据库实例作为驱动程序的一部分,并且只要您保持连接,数据库就会运行。但对于 Hibernate 来说,它喜欢给它一个 DataSource,它实际上应该是一个池数据源。
上面的答案是正确的,将 Derby 作为网络数据库服务器启动确实是一个好主意,即使您位于同一个 JVM 中。您仍然可以使用嵌入式 JDBC 驱动程序,它似乎知道您连接的数据库何时处于网络模式并进行相应调整。 (这还允许使用第三方工具连接到数据库并在运行时查看和编辑数据和模式,非常方便调试。)
System.setProperty("derby.system.home", applicationHome);
NetworkServerControl serverControl = new NetworkServerControl(InetAddress.getByName(m_address),port);
serverControl.start(new PrintWriter(System.out, true));
一旦数据库运行,您就可以将 DataSource
实例粘贴到 JNDI 注册表中。然后 Hibernate 可以从 JNDI 注册表访问该数据源(根据其名称)。
EmbeddedConnectionPoolDataSource40 dataSource = new EmbeddedConnectionPoolDataSource40();
dataSource.setDatabaseName(databaseName);
dataSource.setUser(username);
dataSource.setPassword(password);
EmbeddedConnectionPoolDataSource40
是与池数据源包装器一起使用的数据源实现,因此可以在可能的情况下重用连接。我使用了 Apache Commons DBCP,并修改了 examples 之一使用 EmbeddedConnectionPoolDataSource40 创建我自己的池数据源。
关于java - 如何将 Java DB(名为 Derby)与 hibernate 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1519843/