Java Derby嵌入式数据库未持久化
我正在尝试将嵌入式数据库derby与spring框架结合使用。我可以插入数据并读取它。除了一件数据库没有持久化的事情外,所有的工作都完全正常。当我关闭应用程序并再次运行时,数据不存在。我猜数据库是再次创建的,但不知道为什么 我的代码:Java Derby嵌入式数据库未持久化,java,spring,derby,embedded-database,Java,Spring,Derby,Embedded Database,我正在尝试将嵌入式数据库derby与spring框架结合使用。我可以插入数据并读取它。除了一件数据库没有持久化的事情外,所有的工作都完全正常。当我关闭应用程序并再次运行时,数据不存在。我猜数据库是再次创建的,但不知道为什么 我的代码: @Configuration @ComponentScan @EnableAutoConfiguration public class MainClass { @Bean public DataSource dataSource() {
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class MainClass
{
@Bean
public DataSource dataSource()
{
// no need shutdown, EmbeddedDatabaseFactoryBean will take care of this
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.DERBY) //.HSQL, .H2 or .DERBY
.setName("some-db")
.addScript("/create-db.sql")
.build();
return db;
}
@Bean
public DataSourceTransactionManager transactionManager(DataSource dataSource)
{
DataSourceTransactionManager d = new DataSourceTransactionManager(dataSource);
return d;
}
public static void main(String[] args)
{
ConfigurableApplicationContext context = new SpringApplicationBuilder(MainClass.class).headless(false).run(args);
MainFrame.mf = context.getBean(MainFrame.class);
MainFrame.mf.setVisible(true);
UserService userService = new UserService();
if(userService.isSignedIn())
{
MainFrame.mf.loggedIn();
}
else
{
MainFrame.mf.loggedOut();
}
}
}
spring的输出日志是
2017-09-17 20:41:53.461 INFO 3516 --- [ main] com.some.MainClass : Starting MainClass on maker with PID 3516 (C:\..\NetbeansProjects\..\target\classes started by verma in C:\..\NetbeansProjects\proj)
2017-09-17 20:41:53.469 INFO 3516 --- [ main] com.some.MainClass : No active profile set, falling back to default profiles: default
2017-09-17 20:41:53.571 INFO 3516 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2df32bf7: startup date [Sun Sep 17 20:41:53 IST 2017]; root of context hierarchy
2017-09-17 20:41:56.974 INFO 3516 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-09-17 20:41:57.007 INFO 3516 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2017-09-17 20:41:57.010 INFO 3516 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16
2017-09-17 20:41:57.278 INFO 3516 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-09-17 20:41:57.279 INFO 3516 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3714 ms
2017-09-17 20:41:57.606 INFO 3516 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-09-17 20:41:57.616 INFO 3516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-17 20:41:57.618 INFO 3516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-17 20:41:57.618 INFO 3516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-17 20:41:57.619 INFO 3516 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-17 20:41:58.028 INFO 3516 --- [ main] o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:derby:memory:some-db;create=true', username='sa'
2017-09-17 20:41:58.883 INFO 3516 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executing SQL script from class path resource [create-db.sql]
2017-09-17 20:41:59.248 INFO 3516 --- [ main] o.s.jdbc.datasource.init.ScriptUtils : Executed SQL script from class path resource [create-db.sql] in 365 ms.
2017-09-17 20:42:00.907 INFO 3516 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2df32bf7: startup date [Sun Sep 17 20:41:53 IST 2017]; root of context hierarchy
2017-09-17 20:42:01.052 INFO 3516 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login],methods=[POST]}" onto java.util.Map com.some.connection.ConnectionController.login(java.lang.String)
2017-09-17 20:42:01.055 INFO 3516 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/logout],methods=[POST]}" onto org.springframework.http.ResponseEntity com.some.connection.ConnectionController.logout(java.lang.String)
2017-09-17 20:42:01.062 INFO 3516 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-09-17 20:42:01.063 INFO 3516 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-09-17 20:42:01.153 INFO 3516 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-17 20:42:01.155 INFO 3516 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-17 20:42:01.250 INFO 3516 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-09-17 20:42:01.717 INFO 3516 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-09-17 20:42:01.829 INFO 3516 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-09-17 20:42:01.840 INFO 3516 --- [ main] com.some.MainClass : Started MainClass in 9.034 seconds (JVM running for 9.794)
2017-09-17 20:42:06.305 INFO 3516 --- [ Thread-6] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2df32bf7: startup date [Sun Sep 17 20:41:53 IST 2017]; root of context hierarchy
2017-09-17 20:42:06.314 INFO 3516 --- [ Thread-6] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
2017-09-17 20:42:06.348 INFO 3516 --- [ Thread-6] o.s.j.d.e.EmbeddedDatabaseFactory : Shutting down embedded database: url='jdbc:derby:memory:some-db;create=true'
解决方案:
被接受的答案指向正确的方向,但错误是几分贝;create=true无法启动。然后我查看了Netbeans IDE是如何创建derby连接的。问题是create=true,我认为它不应该与url一起发送,而是与如下代码所示的属性一起发送:
@Bean
public DataSource dataSource()
{
DriverManagerDataSource dm = new DriverManagerDataSource("jdbc:derby:some-db", "root", "root");
Properties properties = new Properties();
properties.setProperty("create", "true");
dm.setConnectionProperties(properties);
dm.setSchema("APP");
dm.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
return dm;
}
@Bean(name="Application.dataSourceInitializer")
public DataSourceInitializer dataSourceInitializer(DataSource dataSource)
{
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
try
{
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.queryForList("SELECT id FROM table_connection");
}
catch(Exception e)
{
initializer.setDatabasePopulator(databasePopulator());
}
return initializer;
}
@Value("classpath:create-db.sql")
private Resource schemaScript;
private DatabasePopulator databasePopulator()
{
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(schemaScript);
return populator;
}
脚本create-db.sql可能会给出错误,如果表在derby中已经存在,那么将其包装在try-catch中
Bean datasourceInitializer被显式命名为“Application.datasourceInitializer”,因为spring自动配置倾向于覆盖它。检查它这是问题的核心:
jdbc:derby:memory:somedb;create=true
当您在Derby JDBC连接URL中说“memory”时,您明确地告诉Derby创建一个非持久数据库
如果从JDBC Connectino URL中删除“memory:”,Derby将在硬盘上的“some db”目录中创建一个持久、持久的数据库。这是问题的核心:
JDBC:Derby:memory:some db;create=true
当您在Derby JDBC连接URL中说“memory”时,您明确地告诉Derby创建一个非持久数据库
如果从JDBC Connectino URL中删除“memory:”,Derby将在硬盘上的“some db”目录中创建一个持久、持久的数据库。因为它是嵌入式的。嵌入式数据库,如不需要服务器且嵌入到应用程序中的数据库。这意味着一切都由应用程序管理。如果你关闭了你的应用程序,你的数据库也会关闭。好吧,那么,如果我需要数据来保存,但不想在客户端机器上安装任何服务器,那么该怎么办呢?对于web服务来说,数据并不是那么重要。通过学习Derby教程来学习Derby的基础知识:因为它是嵌入式的。嵌入式数据库,如不需要服务器且嵌入到应用程序中的数据库。这意味着一切都由应用程序管理。如果你关闭你的应用程序,你的数据库也会关闭。好的,因此,如果我需要数据持久化,但不想在客户机上安装任何服务器,并且数据对于web服务来说并不那么重要,那么该怎么办呢?通过学习Derby教程了解Derby的基础知识:如何使用jdbc创建无内存数据库:嵌入式数据库?我正在尝试使用DataSourceBean的传统方法,并将其传递给jdbc:初始化数据库,但它的意思是启动数据库失败C:\some db?create=true也许您可以用“启动数据库失败”问题开始一个新问题(在研究相关问题之后),因为这是一个不同于您最初的问题。我在编辑中提到过这一点。无法启动数据库的某些数据库;由于“create=true”属性而发生“create=true”。它不应该与db url一起发送,而是与Properties对象一起发送。我在任何地方都找不到它,而是通过查看Netbeans中的derby数据库属性以及它是如何创建数据库的来理解它的。我应该为这个问题开始一个新的问题并回答它吗?如何使用jdbc创建无内存数据库:嵌入式数据库?我正在尝试使用DataSourceBean的传统方法,并将其传递给jdbc:初始化数据库,但它的意思是启动数据库失败C:\some db?create=true也许您可以用“启动数据库失败”问题开始一个新问题(在研究相关问题之后),因为这是一个不同于您最初的问题。我在编辑中提到过这一点。无法启动数据库的某些数据库;由于“create=true”属性而发生“create=true”。它不应该与db url一起发送,而是与Properties对象一起发送。我在任何地方都找不到它,而是通过查看Netbeans中的derby数据库属性以及它是如何创建数据库的来理解它的。我应该为这个问题开始一个新问题并回答它吗?
@Bean
public DataSource dataSource()
{
DriverManagerDataSource dm = new DriverManagerDataSource("jdbc:derby:some-db", "root", "root");
Properties properties = new Properties();
properties.setProperty("create", "true");
dm.setConnectionProperties(properties);
dm.setSchema("APP");
dm.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
return dm;
}
@Bean(name="Application.dataSourceInitializer")
public DataSourceInitializer dataSourceInitializer(DataSource dataSource)
{
final DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource);
try
{
JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.queryForList("SELECT id FROM table_connection");
}
catch(Exception e)
{
initializer.setDatabasePopulator(databasePopulator());
}
return initializer;
}
@Value("classpath:create-db.sql")
private Resource schemaScript;
private DatabasePopulator databasePopulator()
{
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(schemaScript);
return populator;
}