使用基于 Java 的配置在服务器模式下设置 H2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20156827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setup H2 in Server Mode using Java Based Configuration
提问by yousafsajjad
I have spring XML that enables me to start H2 database in server mode using the following configuration:
我有 spring XML,它使我能够使用以下配置在服务器模式下启动 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>
I want to convert to java based configuration. I have seem a post here: Start and setup in-memory DB using Springasking 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-supportfor 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.
我想转换为基于 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 模式设置为服务器模式。它仅以“内存”模式为我启动服务器。
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?
也许使用 EmbeddedDatabaseBuilder(ResourceLoader) 可能会起作用。有没有人有一些示例代码?
采纳答案by yousafsajjad
Here's the code that will allow you start H2 database in server mode using java based spring configuration:
这是允许您使用基于 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;
}
回答by Lucas Oliveira
I belive that's not possible. The term Embedded database as in databases that do not need a server, and are embedded in an application, so you should not use EmbeddedDatabase for this.
我相信这是不可能的。嵌入式数据库一词在不需要服务器的数据库中使用,并且嵌入在应用程序中,因此您不应为此使用 EmbeddedDatabase。
In h2 documentation the term "Embedded" is enfatized with "Local" - (Connecting to an Embedded (Local) Database), and when they use "Server" they talk about remote connections where the database is managed by an server. To reinforce the idea EmbeddedDataSource interface adds only a single method that is not present in the datasource "shutdown" interface that is used to shut down the database usually when the application is shutdown, via @Bean(destroyMethod="shutdown")
for instance.
在 h2 文档中,术语“嵌入式”与“本地”-(连接到嵌入式(本地)数据库)有关,当他们使用“服务器”时,他们谈论的是远程连接,其中数据库由服务器管理。为了加强这个想法,EmbeddedDataSource 接口只添加了一个在数据源“关闭”接口中不存在的方法,该方法通常在应用程序关闭时用于关闭数据库@Bean(destroyMethod="shutdown")
,例如通过。
See more details in here:
在此处查看更多详细信息:
http://h2database.com/html/features.html#database_url
http://h2database.com/html/features.html#database_url