2017-04-26 85 views
1

我有一個項目,我在Hibernate中使用spring-data-jpa,我正在編寫一個在內存數據庫中使用H2的集成測試。Spring-Data-Jpa DriverManagerDataSource初始化兩次

在我的數據庫腳本,創建表後,我居然跑幾個insert語句

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

運行我的集成測試後,我看到有4個條目返回的2

代替

日誌證實測試正在初始化數據源兩次,我想明白爲什麼。任何幫助,將不勝感激。

2017-04-26 12:19:38; LOG_LEVEL = 「DEBUG」; SOURCE = 「org.springframework.jdbc.datasource.DriverManagerDataSource」; EVENT_MESSAGE = 「創建新的JDBC的DriverManager連接到[JDBC:H2:MEM:測試; DB_CLOSE_DELAY = -1; INIT = RUNSCRIPT FROM '類路徑:創db.sql']」

在我的Spring配置文件,與@EnableJpaRepositories註釋,我然後創建相關豆持久性如下所示

@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; 
}  

的屬性是

db.driver = org.h2.Driver 
db.url = jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM \'classpath:create-db.sql\' 
db.username = cost 
db.password = cost 
entitymanager.packages.to.scan = com.somecompany.cost 
hibernate.dialect = org.hibernate.dialect.H2Dialect 
hibernate.show_sql = false 
hibernate.format_sql = false 

乾杯 克里斯

+0

你會告訴你hibProperties()方法,和你的DATABASE_URL –

+0

我已經編輯原始帖子,包括他們..謝謝 – krisrr3

回答

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] 
0

ÿ你不需要創建你自己的數據源,Spring已經在做這個。你的數據源是第二個。這就是您的腳本運行兩次的原因,因爲JDBC URL被調用了兩次。

我建議:

1)刪除您數據源的bean。

2)改變你的JDBC URL來

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

3)重命名創建-data.sql到data.sql並將其添加到您的資源目錄,以便它在類路徑中。

春天應該做的其餘的手冊請參閱here