//by yan 20211229
H2数据库误删Saved Settings
装上H2数据库后,操作失误,把Saved Settings列表里面的项目删除了,并进行了保存。想要恢复原设置的时候,发现卸载后重装也不行。
解决方法:
删除 ~/.h2.server.properties文件,重装H2即可
注意:
找不到路径的同学搜索“h2.server.properties”。
~应该是指就是操作系统当前用户的目录 自行baidu 【~】的含义。
H2的运行方式
-
一、在内存中运行
数据库只在内存中运行,关闭连接后数据库将被清空,适合测试环境
连接字符串:
jdbc:h2:mem:DBName;DB_CLOSE_DELAY=-1
如果不指定DBName,则以私有方式启动,只允许一个连接 -
二、嵌入式
数据库持久化存储为单个文件
连接字符串:
jdbc:h2:file:~/.h2/DBName;AUTO_SERVER=TRUE
~/.h2/DBName表示数据库文件的存储位置,如果第一次连接则会自动创建数据库。
严格来说应该叫文件方式连接,因为文件方式启动不需要先启动数据库服务器,直接在连接URL中填写绝对或相对路径即可,由于创建数据库连接时,H2引擎发现指定的路径下如果不存在数据库,则会自动创建。 -
三、服务模式
H2支持三种服务模式:
1.web server:此种运行方式支持使用浏览器访问H2 Console
2.TCP server:支持客户端/服务器端的连接方式
3.PG server:支持PostgreSQL客户端
启动tcp服务连接字符串示例:
jdbc:h2:tcp://localhost/~/test 使用用户主目录
jdbc:h2:tcp://localhost//data/test 使用绝对路径 -
四、连接字符串参数
1.DB_CLOSE_DELAY:要求最后一个正在连接的连接断开后,不要关闭数据库
2.MODE=MySQL:兼容模式,H2兼容多种数据库,该值可以为:DB2、Derby、HSQLDB、MSSQLServer、MySQL、Oracle、PostgreSQL
3.AUTO_RECONNECT=TRUE:连接丢失后自动重新连接
4.AUTO_SERVER=TRUE:启动自动混合模式,允许开启多个连接,该参数不支持在内存中运行模式
5.TRACE_LEVEL_SYSTEM_OUT、TRACE_LEVEL_FILE:输出跟踪日志到控制台或文件, 取值0为OFF,1为ERROR(默认值),2为INFO,3为DEBUG
6.SET TRACE_MAX_FILE_SIZE mb:设置跟踪日志文件的大小,默认为16M -
五、启动服务模式,打开H2 Console web页面
启动服务,在命令行中执行
java -cp h2*.jar org.h2.tools.Server
执行如下命令,获取选项列表及默认值
java -cp h2*.jar org.h2.tools.Server -?
常见的选项如下:
-web:启动支持H2 Console的服务
-webPort :服务启动端口,默认为8082
-browser:启动H2 Console web管理页面
-tcp:使用TCP server模式启动
-pg:使用PG server模式启动
Activiti里的测试用例与h2数据库
Activiti里的测试用例创建的h2数据库看不到
JPAVariableTest
JPAEnhancedVariableTest
Activiti里的测试用例创建的h2数据库看不到,不知为何?
打了断点,到创建表的代码,确定连接都一致。就是看不到创建的表出来。
原因跟H2的运行方式有关。
将几个数据源连接配置修改一下:
再跑下JPAVariableTest,就可以看到数据库表了。
可是又有新问题:activiti-jpa数据库可以看到,但是activiti和JpaVariableTest数据库怎么都看不到表。好奇怪??
改成服务器连接模式也不行:
//D:\git\Activiti\activiti-core\activiti-engine\src\test\resources\activiti.cfg.xml
<property name="jdbcUrl" value="jdbc:h2:tcp://localhost/~/.h2/db/activiti;AUTO_SERVER=TRUE" />
//D:\git\Activiti\activiti-core\activiti-engine\src\test\resources\org\activiti\standalone\jpa\activiti.cfg.xml
<property name="jdbcUrl" value="jdbc:h2:tcp://localhost/~/.h2/db/JpaVariableTest;AUTO_SERVER=TRUE" />
//D:\git\Activiti\activiti-core\activiti-engine\src\test\resources\META-INF\persistence.xml
<property name="javax.persistence.jdbc.url" value="jdbc:h2:tcp://localhost/~/.h2/db/activiti-jpa;AUTO_SERVER=TRUE" />
直接JPAVariableTest跑不过,而且只有activiti-jpa能创建成功,其它两个都创建不成功。
08:15:03 [main] ERROR org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl - Exception while initializing Database connection
org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/YY/.h2/db/JpaVariableTest" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:622)
at org.h2.engine.SessionRemote.done(SessionRemote.java:611)
at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:147)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:435)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:321)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:173)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:152)
at org.h2.Driver.connect(Driver.java:69)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:190)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:201)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:196)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:93)
at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:403)
at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:89)
at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.initDatabaseType(ProcessEngineConfigurationImpl.java:1128)
at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.initDataSource(ProcessEngineConfigurationImpl.java:1078)
at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.init(ProcessEngineConfigurationImpl.java:865)
at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl.java:848)
at org.activiti.standalone.jpa.JPAVariableTest.initializeProcessEngine(JPAVariableTest.java:76)
at org.activiti.engine.impl.test.AbstractActivitiTestCase.runBare(AbstractActivitiTestCase.java:89)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
08:15:04 [main] INFO org.hibernate.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [
name: activiti-jpa-pu
...]
08:15:04 [main] INFO org.hibernate.Version - HHH000412: Hibernate Core {5.3.15.Final}
Hibernate: create table SHORT_ID_ENTITY (ID_ smallint not null, primary key (ID_))
Hibernate: create table SQLDATE_ID_ENTITY (ID_ date not null, primary key (ID_))
Hibernate: create table STRING_ID_ENTITY (ID_ varchar(255) not null, primary key (ID_))
08:15:05 [main] INFO org.hibernate.tool.schema.internal.SchemaCreatorImpl - HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@240f350a'
08:15:05 [main] INFO bitronix.tm.BitronixTransactionManager - Bitronix Transaction Manager version 2.1.4
08:15:05 [main] INFO bitronix.tm.Configuration - JVM unique ID: <activiti-spring-btm>
08:15:05 [main] INFO bitronix.tm.recovery.Recoverer - recovery committed 0 dangling transaction(s) and rolled back 0 aborted transaction(s) on 0 resource(s) [] (restricted to serverId 'activiti-spring-btm')
08:15:05 [main] ERROR org.activiti.engine.impl.interceptor.CommandContext - Error while closing command context
org.activiti.engine.ActivitiException: couldn't check if tables are already present using metadata:
### Error getting a new connection. Cause: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/YY/.h2/db/JpaVariableTest" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-204]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:697)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
at org.h2.message.DbException.get(DbException.java:227)
at org.h2.message.DbException.get(DbException.java:203)
at org.h2.engine.Engine.throwNotFound(Engine.java:189)
at org.h2.engine.Engine.openSession(Engine.java:72)
at org.h2.engine.Engine.openSession(Engine.java:222)
at org.h2.engine.Engine.createSession(Engine.java:201)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:175)
at java.lang.Thread.run(Unknown Source)
### Cause: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/YY/.h2/db/JpaVariableTest" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-204]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:697)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
at org.h2.message.DbException.get(DbException.java:227)
at org.h2.message.DbException.get(DbException.java:203)
at org.h2.engine.Engine.throwNotFound(Engine.java:189)
at org.h2.engine.Engine.openSession(Engine.java:72)
at org.h2.engine.Engine.openSession(Engine.java:222)
at org.h2.engine.Engine.createSession(Engine.java:201)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:175)
at java.lang.Thread.run(Unknown Source)
at org.activiti.engine.impl.db.DbSqlSession.isTablePresent(DbSqlSession.java:1183)
at org.activiti.engine.impl.db.DbSqlSession.isEngineTablePresent(DbSqlSession.java:1124)
at org.activiti.engine.impl.db.DbSqlSession.dbSchemaUpdate(DbSqlSession.java:1046)
at org.activiti.engine.impl.db.DbSqlSession.performSchemaOperationsProcessEngineBuild(DbSqlSession.java:1454)
at org.activiti.engine.impl.SchemaOperationsProcessEngineBuild.execute(SchemaOperationsProcessEngineBuild.java:28)
at org.activiti.engine.impl.interceptor.CommandInvoker$1.run(CommandInvoker.java:36)
at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:77)
at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:56)
at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:41)
at org.activiti.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:48)
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:59)
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:29)
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:44)
at org.activiti.engine.impl.ProcessEngineImpl.<init>(ProcessEngineImpl.java:68)
at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl.java:849)
at org.activiti.standalone.jpa.JPAVariableTest.initializeProcessEngine(JPAVariableTest.java:76)
at org.activiti.engine.impl.test.AbstractActivitiTestCase.runBare(AbstractActivitiTestCase.java:89)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error getting a new connection. Cause: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/YY/.h2/db/JpaVariableTest" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-204]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:697)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
at org.h2.message.DbException.get(DbException.java:227)
at org.h2.message.DbException.get(DbException.java:203)
at org.h2.engine.Engine.throwNotFound(Engine.java:189)
at org.h2.engine.Engine.openSession(Engine.java:72)
at org.h2.engine.Engine.openSession(Engine.java:222)
at org.h2.engine.Engine.createSession(Engine.java:201)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:175)
at java.lang.Thread.run(Unknown Source)
### Cause: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/YY/.h2/db/JpaVariableTest" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-204]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:697)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:496)
at org.h2.message.DbException.get(DbException.java:227)
at org.h2.message.DbException.get(DbException.java:203)
at org.h2.engine.Engine.throwNotFound(Engine.java:189)
at org.h2.engine.Engine.openSession(Engine.java:72)
at org.h2.engine.Engine.openSession(Engine.java:222)
at org.h2.engine.Engine.createSession(Engine.java:201)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:175)
at java.lang.Thread.run(Unknown Source)
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getConnection(DefaultSqlSession.java:300)
at org.activiti.engine.impl.db.DbSqlSession.isTablePresent(DbSqlSession.java:1140)
... 33 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database "C:/Users/YY/.h2/db/JpaVariableTest" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:622)
at org.h2.engine.SessionRemote.done(SessionRemote.java:611)
at org.h2.engine.SessionRemote.initTransfer(SessionRemote.java:147)
at org.h2.engine.SessionRemote.connectServer(SessionRemote.java:435)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:321)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:173)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:152)
at org.h2.Driver.connect(Driver.java:69)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:190)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:201)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:196)
at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:93)
at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:403)
at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:89)
at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:138)
at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:60)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getConnection(DefaultSqlSession.java:298)
... 34 common frames omitted
报错原因:
以上图其实也不是原因。
分析下报错堆栈,你是另外两个数据库没办法自动创建。我于是想给它手动创建创建。
h2手动创建好很坑,用jdbc:h2:tcp:的方式登录不上,只能用jdbc:h2:file:的方式可以自动创建 :
生成文件 后再用jdbc:h2:tcp:方式登录就可以进去了。而且JPAVariableTest也成功跑通。idea 也能成功边上各个数据库了。