java server模式 设置,使用基于Java的配置在服务器模式下设置H2

I have spring XML that enables me to start H2 database in server mode using the following configuration:

destroy-method="close">

create-drop

true

I want to convert to java based configuration. I have seem a post here: Start and setup in-memory DB using Spring asking somewhat the same question and I have looked at http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html#jdbc-embedded-database-support for Embedded Database but it does not say how to set H2 mode to Server mode. It is starting the server for me in "mem" mode only.

I have the following code:

EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();

builder.setType(EmbeddedDatabaseType.H2);

builder.setName(DATABASE_NAME);

builder.addScript(H2_SCHEMA);

builder.addScript(H2_TEST);

return builder.build();

Maybe using EmbeddedDatabaseBuilder(ResourceLoader) might work. Does anyone has some sample code for it?

解决方案

Here's the code that will allow you start H2 database in server mode using java based spring configuration:

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值