2013-11-23 21 views
4

我有春天XML,它使我使用下面的配置來啓動服務器模式H2數據庫:安裝H2

<beans profile="test-h2"> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
      destroy-method="close"> 
     <property name="driverClassName" value="org.h2.Driver"/> 
     <property name="url" value="jdbc:h2:target/h2/pps;AUTO_SERVER=TRUE"/> 
     <property name="username" value="sa"/> 
     <property name="password" value=""/> 
    </bean> 
    <bean id="entityManagerFactory" parent="entityManagerFactoryCommonParent"> 
     <property name="jpaProperties"> 
      <props> 
       <prop key="hibernate.hbm2ddl.auto">create-drop</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 
    </bean> 

我想轉換爲基於Java的配置。我在這裏似乎有一個帖子:Start and setup in-memory DB using Spring問有點相同的問題,我已經看過http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database-support嵌入式數據庫,但它沒有說如何將H2模式設置爲服務器模式。它僅以「mem」模式啓動服務器。

我有以下代碼:

EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); 

builder.setType(EmbeddedDatabaseType.H2); 
builder.setName(DATABASE_NAME); 
builder.addScript(H2_SCHEMA); 
builder.addScript(H2_TEST); 
return builder.build(); 
也許使用EmbeddedDatabaseBuilder(資源加載)

可能會奏效。有沒有人有一些示例代碼?

回答

7

下面的代碼,可以讓你使用基於Java Spring配置開始在服務器模式H2數據庫:

private static final String H2_JDBC_URL_TEMPLATE = "jdbc:h2:%s/target/db/sample;AUTO_SERVER=TRUE"; 
@Value("classpath:seed-data.sql") 
private Resource H2_SCHEMA_SCRIPT; 

@Value("classpath:test-data.sql") 
private Resource H2_DATA_SCRIPT; 

@Value("classpath:drop-data.sql") 
private Resource H2_CLEANER_SCRIPT; 


@Bean 
public DataSource dataSource(Environment env) throws Exception { 
     return createH2DataSource(); 
} 


@Autowired 
@Bean 
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { 

    final DataSourceInitializer initializer = new DataSourceInitializer(); 
    initializer.setDataSource(dataSource); 
    initializer.setDatabasePopulator(databasePopulator()); 
    initializer.setDatabaseCleaner(databaseCleaner()); 
    return initializer; 
} 


private DatabasePopulator databasePopulator() { 
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); 
    populator.addScript(H2_SCHEMA_SCRIPT); 
    populator.addScript(H2_DATA_SCRIPT); 
    return populator; 
} 

private DatabasePopulator databaseCleaner() { 
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); 
    populator.addScript(H2_CLEANER_SCRIPT); 
    return populator; 
} 

private DataSource createH2DataSource() { 
    String jdbcUrl = String.format(H2_JDBC_URL_TEMPLATE, System.getProperty("user.dir")); 
    JdbcDataSource ds = new JdbcDataSource();  
    ds.setURL(jdbcUrl); 
    ds.setUser("sa"); 
    ds.setPassword(""); 

    return ds; 
} 

@Bean 
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { 
    JpaTransactionManager transactionManager = new JpaTransactionManager(); 
    transactionManager.setEntityManagerFactory(entityManagerFactory); 
    return transactionManager; 
} 

@Bean 
public LocalContainerEntityManagerFactoryBean entityManagerFactory(Environment env) throws Exception { 
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
    vendorAdapter.setGenerateDdl(Boolean.TRUE); 
    vendorAdapter.setShowSql(Boolean.TRUE);  

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 
    factory.setPersistenceUnitName("sample"); 
    factory.setJpaVendorAdapter(vendorAdapter); 
    factory.setPackagesToScan("com.sample.model"); 
    factory.setDataSource(dataSource(env));  

    factory.setJpaProperties(jpaProperties()); 
    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver()); 

    return factory; 
} 

Properties jpaProperties() { 
    Properties props = new Properties(); 
    props.put("hibernate.query.substitutions", "true 'Y', false 'N'"); 
    props.put("hibernate.hbm2ddl.auto", "create-drop"); 
    props.put("hibernate.show_sql", "false"); 
    props.put("hibernate.format_sql", "true"); 

    return props; 
} 
+0

截至目前 - 這是完整的矯枉過正。同樣的事情可以通過幾行完成。 https://springframework.guru/using-the-h2-database-console-in-spring-boot-with-spring-security/我不以任何方式與作者有關​​ - 但他的指示肯定對我有用。 –

1

我相信這是不可能的。術語嵌入式數據庫與不需要服務器的數據庫一樣,嵌入在應用程序中,因此您不應該使用EmbeddedDatabase。

在h2文檔中,術語「嵌入式」用「本地」 - (連接到嵌入式(本地)數據庫)進行了enfatized,並且當它們使用「服務器」時,它們討論數據庫由服務器管理的遠程連接。爲了強化這個想法,EmbeddedDataSource接口只添加了一個單獨的方法,該方法通常在應用程序關閉時通過@Bean(destroyMethod="shutdown")來關閉數據庫「關閉」接口中不存在的數據源。

看到更多細節在這裏:

http://h2database.com/html/features.html#database_url

When is a database called as an Embedded database?

http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/jdbc/datasource/embedded/EmbeddedDatabase.html