当我在存储库中编写一些 JPA 查询时,Spring 启动停止

Posted

技术标签:

【中文标题】当我在存储库中编写一些 JPA 查询时,Spring 启动停止【英文标题】:Spring boot stops as I write some JPA queries in the repository 【发布时间】:2016-08-01 05:39:53 【问题描述】:

我正在使用带有 H2 数据库的 Spring Boot。我的应用程序在控制台上以以下文本停止。

2016-08-01 10:36:08.610  INFO 3920 --- [ost-startStop-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2016-08-01 10:36:08.651  INFO 3920 --- [ost-startStop-1] org.hibernate.Version                    : HHH000412: Hibernate Core 4.3.11.Final
2016-08-01 10:36:08.651  INFO 3920 --- [ost-startStop-1] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2016-08-01 10:36:08.651  INFO 3920 --- [ost-startStop-1] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2016-08-01 10:36:08.801  INFO 3920 --- [ost-startStop-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 4.0.5.Final
2016-08-01 10:36:09.031  INFO 3920 --- [ost-startStop-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2016-08-01 10:36:09.131  INFO 3920 --- [ost-startStop-1] o.h.h.i.ast.ASTQueryTranslatorFactory    : HHH000397: Using ASTQueryTranslatorFactory
2016-08-01 10:36:09.291  INFO 3920 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2016-08-01 10:36:09.291  INFO 3920 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000102: Fetching database metadata
2016-08-01 10:36:09.301  INFO 3920 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000396: Updating schema
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: TEST.PUBLIC.EMAILDATA
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [quantity, emailto, id, autostart, emailfrom]
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: []
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [primary_key_2]
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000261: Table found: TEST.PUBLIC.STATUS
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000037: Columns: [exchangefile, consolidate, processdate, aggregateall, scriptmaster, sendmail, id, insiderstrade]
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000108: Foreign keys: []
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] o.hibernate.tool.hbm2ddl.TableMetadata   : HHH000126: Indexes: [primary_key_9]
2016-08-01 10:36:09.311  INFO 3920 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000232: Schema update complete

我的仓库是:

@Transactional
public interface EmailDataRepository extends JpaRepository<EmailData,Long>
      @Query("SELECT p FROM EmailData P")
      public EmailData findLastRow();

而 DBConfig 是:

@Configuration
@EnableTransactionManagement
@ComponentScan("com.demo.*")
@PropertySource("classpath:application.properties")
@EntityScan(basePackages = "com.demo.model")
@EnableJpaRepositories("com.demo.repository.main")
public class DBConfig ....

型号:

@Entity
@Table(name="EmailData")
public class EmailData 

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    private String emailTo;
    private String emailFrom;   
    private String quantity;
    private Boolean autoStart;
...

【问题讨论】:

【参考方案1】:

您的H2 数据库网址应具有以下配置

DB_CLOSE_ON_EXIT=FALSE

Spring Boot 文档有一个引用:

如果出于某种原因,您确实为嵌入式数据库配置了连接 U​​RL,则应注意确保禁用数据库的自动关闭功能。如果你使用 H2,你应该使用 DB_CLOSE_ON_EXIT=FALSE 来这样做。如果您使用的是 HSQLDB,则应确保不使用 shutdown=true。禁用数据库的自动关闭允许 Spring Boot 控制数据库何时关闭,从而确保在不再需要访问数据库时发生这种情况。

【讨论】:

以上是关于当我在存储库中编写一些 JPA 查询时,Spring 启动停止的主要内容,如果未能解决你的问题,请参考以下文章

如何为多个实体编写单个 jpa 规范

当我在存储库中丢失一些修订时,如何转储所有 svn 数据?

在 spring data jpa 存储库中搜索许多可选参数

spring jpa存储库中的子查询

使用 Spring JPA 调用存储过程

使用 concat() 时 JPA 本机查询返回字节数组而不是字符串