java 无法从底层数据库获取连接!-- HSQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4117764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connections could not be acquired from th e underlying database! -- HSQL DB
提问by javanoob
I am trying to learn Hibernate from Java Persistence with hibernate by Gavin King.
我正在尝试从 Java Persistence 和 Gavin King 的 hibernate 中学习 Hibernate。
I just downloaded the examples given in the book from this location http://downloads.jboss.org/hibernate/caveatemptor/jpwh-gettingstarted-070401.zip
我刚刚从这个位置下载了书中给出的例子http://downloads.jboss.org/hibernate/caveatempor/jpwh-gettingstarted-070401.zip
I started my HSQL db using ant startdb and database started successfully.
我使用 ant startdb 启动了我的 HSQL 数据库,并且数据库成功启动。
When trying to run the target schemaexport I am getting the below error..
尝试运行目标 schemaexport 时,出现以下错误..
schemaexport: [hibernatetool] Executing Hibernate Tool with a Standard Configuration [hibernatetool] 1. task: hbm2ddl (Generates database schema) [hibernatetool] 18:07:56,555 INFO Environment:500 - Hibernate 3.2.0.cr5 [hibernatetool] 18:07:56,555 INFO Environment:533 - hibernate.properties not fo und [hibernatetool] 18:07:56,555 INFO Environment:667 - Bytecode provider name : cg lib [hibernatetool] 18:07:56,571 INFO Environment:584 - using JDK 1.4 java.sql.Time stamp handling [hibernatetool] 18:07:56,617 INFO Configuration:1384 - configuring from file: h ibernate.cfg.xml [hibernatetool] 18:07:56,680 INFO Configuration:507 - Reading mappings from res ource: hello/Message.hbm.xml [hibernatetool] 18:07:56,758 INFO HbmBinder:300 - Mapping class: hello.Message -> MESSAGES [hibernatetool] 18:07:56,851 INFO Configuration:1465 - Configured SessionFactor y: null [hibernatetool] 18:07:56,851 INFO Dialect:141 - Using dialect: org.hibernate.di alect.HSQLDialect [hibernatetool] 18:07:56,914 INFO SchemaExport:154 - Running hbm2ddl schema exp ort [hibernatetool] 18:07:56,914 INFO SchemaExport:174 - writing generated schema t o file: C:\Users\Ramesh\Desktop\Tutorials\hibernate\Hibernate Code samples\jpwh- gettingstarted-070401\helloworld-native\helloworld-ddl.sql [hibernatetool] 18:07:56,914 INFO SchemaExport:179 - exporting generated schema to database [hibernatetool] 18:07:56,914 INFO C3P0ConnectionProvider:50 - C3P0 using driver : org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost [hibernatetool] 18:07:56,914 INFO C3P0ConnectionProvider:51 - Connection proper ties: {user=sa} [hibernatetool] 18:07:56,914 INFO C3P0ConnectionProvider:54 - autocommit mode: false [hibernatetool] 18:08:26,554 WARN BasicResourcePool:1222 - com.mchange.v2.resou rcepool.BasicResourcePool$AcquireTask@61f1680f -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we fai led to succeed more than the maximum number of allowed acquisition attempts (30) . [hibernatetool] 18:08:26,554 ERROR SchemaExport:202 - schema export unsuccessful
[hibernatetool] java.sql.SQLException: Connections could not be acquired from th e underlying database! [hibernatetool] at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.j ava:104) [hibernatetool] at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.che ckoutPooledConnection(C3P0PooledConnectionPool.java:236) [hibernatetool] at com.mchange.v2.c3p0.PoolBackedDataSource.getConnectio n(PoolBackedDataSource.java:94) [hibernatetool] at org.hibernate.connection.C3P0ConnectionProvider.getCo nnection(C3P0ConnectionProvider.java:35) [hibernatetool] at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionH elper.prepare(ManagedProviderConnectionHelper.java:28) [hibernatetool] at org.hibernate.tool.hbm2ddl.SchemaExport.execute(Schem aExport.java:180) [hibernatetool]
at org.hibernate.tool.hbm2ddl.SchemaExport.create(Schema Export.java:133) [hibernatetool]
at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hb m2DDLExporterTask.java:55) [hibernatetool] at org.hibernate.tool.ant.HibernateToolTask.execute(Hibe rnateToolTask.java:171) [hibernatetool] at org.apache.tools.ant.UnknownElement.execute(UnknownEl ement.java:291) [hibernatetool]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native M ethod) [hibernatetool] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMet hodAccessorImpl.java:39) [hibernatetool] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Deleg atingMethodAccessorImpl.java:25) [hibernatetool] at java.lang.reflect.Method.invoke(Method.java:597) [hibernatetool] at org.apache.tools.ant.dispatch.DispatchUtils.execute(D ispatchUtils.java:106) [hibernatetool] at org.apache.tools.ant.Task.perform(Task.java:348) [hibernatetool] at org.apache.tools.ant.Target.execute(Target.java:390) [hibernatetool] at org.apache.tools.ant.Target.performTasks(Target.java: 411) [hibernatetool] at org.apache.tools.ant.Project.executeSortedTargets(Pro ject.java:1397) [hibernatetool]
at org.apache.tools.ant.Project.executeTarget(Project.ja va:1366) [hibernatetool] at org.apache.tools.ant.helper.DefaultExecutor.executeTa rgets(DefaultExecutor.java:41) [hibernatetool] at org.apache.tools.ant.Project.executeTargets(Project.j ava:1249) [hibernatetool] at org.apache.tools.ant.Main.runBuild(Main.java:801) [hibernatetool] at org.apache.tools.ant.Main.startAnt(Main.java:218) [hibernatetool] at org.apache.tools.ant.launch.Launcher.run(Launcher.jav a:280) [hibernatetool] at org.apache.tools.ant.launch.Launcher.main(Launcher.ja va:109) [hibernatetool] Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceExce ption: A ResourcePool could not acquire a resource from its primary factory or s ource. [hibernatetool]
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAc quire(BasicResourcePool.java:970) [hibernatetool] at com.mchange.v2.resourcepool.BasicResourcePool.checkou tResource(BasicResourcePool.java:208) [hibernatetool] at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.che ckoutPooledConnection(C3P0PooledConnectionPool.java:232) [hibernatetool] ... 24 more [hibernatetool] 1 errors occurred while performing . [hibernatetool] Error #1: java.sql.SQLException: Connections could not be acquir ed from the underlying database! [hibernatetool] 18:08:26,569 WARN BasicResourcePool:1222 - com.mchange.v2.resou rcepool.BasicResourcePool$AcquireTask@72f864fe -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we fai led to succeed more than the maximum number of allowed acquisition attempts (30) .`
{user=sa} [hibernatetool] 18:07:56,914 INFO C3P0ConnectionProvider:54 - 自动提交模式:false [hibernatetool] 18:08:26,554 WARN BasicResourcePool:1222 - com.mchange.v2.resou rcepool.Basic1Task8rePool.Basic1Task800$Ac - 获取尝试失败!!!清除挂起的收购。在尝试获取所需的新资源时,我们导致成功超过允许获取尝试的最大次数 (30)。[hibernatetool] 18:08:26,554 错误 SchemaExport:202 - 模式导出失败 我们 fai 导致成功超过允许的最大获取尝试次数 (30)。[hibernatetool] 18:08:26,554 错误 SchemaExport:202 - 模式导出失败 我们 fai 导致成功超过允许的最大获取尝试次数 (30)。[hibernatetool] 18:08:26,554 错误 SchemaExport:202 - 模式导出失败
[hibernatetool] java.sql.SQLException: 无法从底层数据库获取连接![hibernatetool] 在 com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.j ava:104) [hibernatetool] 在 com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.che ckoutPooledConnection(C3P0PooledConnectionPool.java:236) [hibernatetool] ] 在 com.mchange.v2.c3p0.PoolBackedDataSource.getConnectio n(PoolBackedDataSource.java:94) [hibernatetool] 在 org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:35) [hibernatetool] 在 org.hibernate. tool.hbm2ddl.ManagedProviderConnectionH elper.prepare(ManagedProviderConnectionHelper.java:28) [hibernatetool] 在 org.hibernate.tool.hbm2ddl.SchemaExport.execute(Schem aExport.java:180) [hibernatetool]
在 org.hibernate.tool.hbm2ddl.SchemaExport.create(Schema Export.java:133) [hibernatetool]
在 org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hb m2DDLExporterTask.java:55) [hibernatetool] 在 org.hibernate .tool.ant.HibernateToolTask.execute(Hibe rnateToolTask.java:171) [hibernatetool] 在 org.apache.tools.ant.UnknownElement.execute(UnknownEl ement.java:291) [hibernatetool]
在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [hibernatetool] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMet hodAccessorImpl.java:39) [hibernatetool] 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Deleg atingMethod) ) [hibernatetool] 在 java.lang.reflect.Method.invoke(Method.java:597) [hibernatetool] 在 org.apache.tools.ant.dispatch.DispatchUtils.execute(D ispatchUtils.java:106) [hibernatetool] 在org.apache.tools.ant.Task.perform(Task.java:348) [hibernatetool] 在 org.apache.tools.ant.Target.execute(Target.java:390) [hibernatetool] 在 org.apache.tools。 ant.Target.performTasks(Target.java: 411) [hibernatetool] 在 org.apache.tools.ant.Project.executeSortedTargets(Project.java:1397) [hibernatetool]
在 org.apache.tools.ant.Project.executeTarget(Project.ja va:1366) [hibernatetool] 在 org.apache.tools.ant.helper.DefaultExecutor.executeTa rgets(DefaultExecutor.java:41) [hibernatetool] 在 org .apache.tools.ant.Project.executeTargets(Project.j ava:1249) [hibernatetool] 在 org.apache.tools.ant.Main.runBuild(Main.java:801) [hibernatetool] 在 org.apache.tools。 ant.Main.startAnt(Main.java:218) [hibernatetool] 在 org.apache.tools.ant.launch.Launcher.run(Launcher.java a:280) [hibernatetool] 在 org.apache.tools.ant.launch .Launcher.main(Launcher.ja va:109) [hibernatetool] 原因:com.mchange.v2.resourcepool.CannotAcquireResourceExce ption:ResourcePool 无法从其主要工厂或来源获取资源。[休眠工具]
在 com.mchange.v2.resourcepool.BasicResourcePool.awaitAc quire(BasicResourcePool.java:970) [hibernatetool] 在 com.mchange.v2.resourcepool.BasicResourcePool.checkout tResource(BasicResourcePool.java:208) [hibernatetool] 在 com. .v2.c3p0.impl.C3P0PooledConnectionPool.che ckoutPooledConnection(C3P0PooledConnectionPool.java:232) [hibernatetool] ... 24 more [hibernatetool] 1 执行时发生错误。[hibernatetool] 错误 #1:java.sql.SQLException:无法从底层数据库获取连接![hibernatetool] 18:08:26,569 WARN BasicResourcePool:1222 - com.mchange.v2.resou rcepool.BasicResourcePool$AcquireTask@72f864fe - 获取尝试失败!!!清除挂起的收购。在尝试获取所需的新资源时,
I don't have too much knowledge about either hiberate or HSQL..
我对 hiberate 或 HSQL 的了解不多。
Some one please explain whats wrong here??
有人请解释一下这里出了什么问题?
Thanks
谢谢
EDIT:I am trying to run the project helloworld-native
.
编辑:我正在尝试运行该项目helloworld-native
。
The target given in the ant build.xml for startdb is as below:
startdb的ant build.xml中给出的target如下:
<!-- Start the HSQL DB server -->
<target name="startdb" description="Run HSQL database server with clean DB">
<!-- Delete database files -->
<delete dir="${database.dir}"/>
<java classname="org.hsqldb.Server"
fork="yes"
classpathref="project.classpath"
failonerror="true">
<arg value="-database.0"/>
<arg value="file:${database.dir}/db"/>
</java>
</target>
and when i run the command ant startdb
i see the below output:
当我运行命令时,ant startdb
我看到以下输出:
startdb:
[delete] Deleting directory C:\Users\Ramesh\Desktop\Tutorials\hibernate\Hibernate Code samples\jpwh-gettingstarted-070401\helloworld-native\database
[java] [Server@15ded0fd]: [Thread[main,5,main]]: checkRunning(false) entered
[java] [Server@15ded0fd]: [Thread[main,5,main]]: checkRunning(false) exited
[java] [Server@15ded0fd]: Startup sequence initiated from main() method
[java] [Server@15ded0fd]: Loaded properties from [C:\Users\Ramesh\Desktop\Tutorials hibernate\Hibernate Code samples\jpwh-gettingstarted-070401\helloworld-native\server.properties]
[java] [Server@15ded0fd]: Initiating startup sequence...
[java] [Server@15ded0fd]: Server socket opened successfully in 29 ms.
[java] [Server@15ded0fd]: Database [index=0, id=0, db=file:database/db, alias=] opened sucessfully in 469 ms.
[java] [Server@15ded0fd]: Startup sequence completed in 523 ms.
[java] [Server@15ded0fd]: 2010-11-08 22:11:08.027 HSQLDB server 1.8.0 is online
[java] [Server@15ded0fd]: To close normally, connect and execute SHUTDOWN SQL
[java] [Server@15ded0fd]: From command line, use [Ctrl]+[C] to abort abruptly
Then, i think i am running HSQL DB in server mode only (from the above output)..
然后,我想我只在服务器模式下运行 HSQL DB(来自上面的输出)。
Please tell me what else I am missing..
请告诉我我还缺少什么..
采纳答案by Pascal Thivent
Update:After a second look, it appears that I was wrong, you can start HSQL in server mode using the ant script and you're not forced to use the command line. Actually, I tried to reproduce the problem with the helloworld-native
sample but it worked fine for me.
更新:再次查看后,似乎我错了,您可以使用 ant 脚本在服务器模式下启动 HSQL,而不必强制使用命令行。实际上,我试图用helloworld-native
示例重现该问题,但对我来说效果很好。
First, I started HSQLDB in a first shell using ant startdb
:
首先,我在第一个 shell 中使用ant startdb
以下命令启动了 HSQLDB :
$ ant startdb
Buildfile: /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build.xml
startdb:
[java] [Server@a97b0b]: [Thread[main,5,main]]: checkRunning(false) entered
[java] [Server@a97b0b]: [Thread[main,5,main]]: checkRunning(false) exited
[java] [Server@a97b0b]: Startup sequence initiated from main() method
[java] [Server@a97b0b]: Loaded properties from [/home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/server.properties]
[java] [Server@a97b0b]: Initiating startup sequence...
[java] [Server@a97b0b]: Server socket opened successfully in 39 ms.
[java] [Server@a97b0b]: Database [index=0, id=0, db=file:database/db, alias=] opened sucessfully in 1148 ms.
[java] [Server@a97b0b]: Startup sequence completed in 1281 ms.
[java] [Server@a97b0b]: 2010-11-08 18:29:20.094 HSQLDB server 1.8.0 is online
[java] [Server@a97b0b]: To close normally, connect and execute SHUTDOWN SQL
[java] [Server@a97b0b]: From command line, use [Ctrl]+[C] to abort abruptly
Then, I ran ant schemaexport
from another shell:
然后,我ant schemaexport
从另一个 shell 运行:
$ ant schemaexport
Buildfile: /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build.xml
compile:
[mkdir] Created dir: /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build
[javac] /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build.xml:39: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 3 source files to /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build
copymetafiles:
[copy] Copying 3 files to /home/pascal/Projects/jpwh-gettingstarted-070401/helloworld-native/build
schemaexport:
[hibernatetool] Executing Hibernate Tool with a Standard Configuration
[hibernatetool] 1. task: hbm2ddl (Generates database schema)
[hibernatetool]
[hibernatetool] alter table MESSAGES
[hibernatetool] drop constraint FK_NEXT_MESSAGE;
[hibernatetool]
[hibernatetool] drop table MESSAGES if exists;
[hibernatetool]
[hibernatetool] create table MESSAGES (
[hibernatetool] MESSAGE_ID bigint generated by default as identity (start with 1),
[hibernatetool] MESSAGE_TEXT varchar(255),
[hibernatetool] NEXT_MESSAGE_ID bigint,
[hibernatetool] primary key (MESSAGE_ID)
[hibernatetool] );
[hibernatetool]
[hibernatetool] alter table MESSAGES
[hibernatetool] add constraint FK_NEXT_MESSAGE
[hibernatetool] foreign key (NEXT_MESSAGE_ID)
[hibernatetool] references MESSAGES;
[hibernatetool] 1 errors occurred while performing <hbm2ddl>.
[hibernatetool] Error #1: java.sql.SQLException: Table not found: MESSAGES in statement [alter table MESSAGES]
BUILD SUCCESSFUL
Total time: 14 seconds
And indeed, when running ant dbmanager
, I can see the MESSAGES
table as expected.
事实上,在运行时ant dbmanager
,我可以MESSAGES
按预期看到表格。
So everything worked just fine, out of the box. If it doesn't for you, maybe start looking at things such as a networking issue, a personal firewall, etc.
所以一切都很好,开箱即用。如果它不适合您,也许可以开始查看诸如网络问题、个人防火墙等问题。
According to the trace, it looks like HSQL is not started (at least not in server mode) and I suspect that you didn't start it like you are supposed to. To start it in server mode, from the helloworld-reverse
directory, run (as explained in the section Starting the HSQL database system, p 98):
根据跟踪,看起来 HSQL 没有启动(至少不是在服务器模式下),我怀疑你没有像你应该的那样启动它。要以服务器模式启动它,请从helloworld-reverse
目录中运行(如启动 HSQL 数据库系统部分中所述,第 98 页):
$ java -cp lib/hsqldb.jar org.hsqldb.Server
Then you'll be able to connect to it using the jdbc:hsqldb:hsql://localhost
connection string.
然后您将能够使用jdbc:hsqldb:hsql://localhost
连接字符串连接到它。