Spring-Data-Jpa DriverManagerDataSource初始化两次

时间:2017-04-26 10:28:36

标签: hibernate spring-data-jpa h2

我有一个项目,我使用Spring-data-jpa和Hibernate,我正在编写一个在内存数据库中使用H2的集成测试。

在我的数据库脚本中,创建表后,我实际上运行了一些插入语句

    <?php
// Add the code below to your theme's functions.php file to add a confirm password field on the register form under My Accounts.
add_filter('woocommerce_registration_errors', 'registration_errors_validation', 10,3);
function registration_errors_validation($reg_errors, $sanitized_user_login, $user_email) {
    global $woocommerce;
    extract( $_POST );
    if ( strcmp( $password, $password2 ) !== 0 ) {
        return new WP_Error( 'registration-error', __( 'Passwords do not match.', 'woocommerce' ) );
    }
    return $reg_errors;
}
add_action( 'woocommerce_register_form', 'wc_register_form_password_repeat' );
function wc_register_form_password_repeat() {
    ?>
    <p class="form-row form-row-wide">
        <label for="reg_password2"><?php _e( 'Password Repeat', 'woocommerce' ); ?> <span class="required">*</span></label>
        <input type="password" class="input-text" name="password2" id="reg_password2" value="<?php if ( ! empty( $_POST['password2'] ) ) echo esc_attr( $_POST['password2'] ); ?>" />
    </p>
    <?php
}
?>

运行我的集成测试后,我看到返回了4个条目而不是2个。

日志证实测试是初始化数据源两次,我想了解原因。任何帮助将不胜感激。

  

2017-04-26 12:19:38; LOG_LEVEL =&#34; DEBUG&#34 ;; SOURCE =&#34; org.springframework.jdbc.datasource.DriverManagerDataSource&#34 ;; EVENT_MESSAGE =&#34;创建新的JDBC DriverManager连接到[jdbc:h2:mem:test; DB_CLOSE_DELAY = -1; INIT = RUNSCRIPT FROM&#39; classpath:create-db.sql&#39;]&#34; < / p>

在我的Spring配置文件中,用@EnableJpaRepositories注释,然后我创建了持久性相关的bean,如下所示

INSERT INTO COST (paymentType, costValue, costCategory) VALUES ('INTERNATIONAL', 100, 'LICENSES');
INSERT INTO COST (paymentType, costValue, costCategory) VALUES ('INTERNATIONAL', 20, 'HARDWARE');

属性

    @Bean
public JpaTransactionManager transactionManager() {
    if (transactionManager == null) {
        transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    }
    return transactionManager;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    if (entityManagerFactoryBean == null) {

        entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        entityManagerFactoryBean.setPackagesToScan(env.getRequiredProperty(ENTITYMANAGER_PACKAGES_TO_SCAN));
        entityManagerFactoryBean.setJpaProperties(hibProperties());      
    }
    return entityManagerFactoryBean;
}

@Bean
public DriverManagerDataSource dataSource() {

    if (dataSource == null) {
        dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getRequiredProperty(DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(DATABASE_PASSWORD));
    }
    return dataSource;
}    

干杯 克里斯

2 个答案:

答案 0 :(得分:0)

从db.url属性中删除'DB_CLOSE_DELAY = -1'后,我开始获取正确数量的数据行;这是2。

但是,我检查了日志,仍然创建了2个数据源实例,我不确定为什么实例没有被重用

日志行位于

之下

第1616行

2017-04-27 09:41:58; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory"; EVENT_MESSAGE="Registering IdentifierGenerator strategy [enhanced-table] -> [org.hibernate.id.enhanced.TableGenerator]"
2017-04-27 09:41:58; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.cfg.Configuration"; EVENT_MESSAGE="Preparing to build session factory with filters : {}"
2017-04-27 09:41:58; LOG_LEVEL="DEBUG"; SOURCE="org.springframework.jdbc.datasource.DriverManagerDataSource"; EVENT_MESSAGE="Creating new JDBC DriverManager Connection to [jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:create-db.sql']"

第2331行

2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.hql.internal.ast.ErrorCounter"; EVENT_MESSAGE="throwQueryException() : no errors"
2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.SQL"; EVENT_MESSAGE="select cost0_.costId as costId1_0_, cost0_.costCategory as costCate2_0_, cost0_.costValue as costValu3_0_, cost0_.paymentType as paymentT4_0_ from Cost cost0_ where cost0_.paymentType=?"
2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.engine.jdbc.internal.LogicalConnectionImpl"; EVENT_MESSAGE="Obtaining JDBC connection"
2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.springframework.jdbc.datasource.DriverManagerDataSource"; EVENT_MESSAGE="Creating new JDBC DriverManager Connection to [jdbc:h2:mem:test;INIT=RUNSCRIPT FROM 'classpath:create-db.sql']"
2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.engine.jdbc.internal.LogicalConnectionImpl"; EVENT_MESSAGE="Obtained JDBC connection"
2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.loader.Loader"; EVENT_MESSAGE="Result set row: 0"
2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.loader.Loader"; EVENT_MESSAGE="Result row: EntityKey[com.somecompany.domain.Cost#1]"
2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.loader.Loader"; EVENT_MESSAGE="Result set row: 1"
2017-04-27 09:41:59; LOG_LEVEL="DEBUG"; SOURCE="org.hibernate.loader.Loader"; EVENT_MESSAGE="Result row: EntityKey[com.somecompany.domain.domain.Cost#2]

答案 1 :(得分:0)

您不需要创建自己的dataSource,Spring已经在这样做了。您的数据源是第二个。这就是您的脚本运行两次的原因,因为JDBC URL被调用了两次。

我建议:

1)删除dataSource bean。

2)将您的JDBC URL更改为

jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"

3)将create-data.sql重命名为data.sql并将其添加到资源目录中,使其位于类路径中。

Spring应该完成其余的工作,请参阅here手册。