JPA - transactions not being committed

霸气de小男生 提交于 2019-12-06 02:58:41

According to the HSQLDB user guide:

Closing the Database

From version 1.7.2, in-process databases are no longer closed when the last connection to the database is explicitly closed via JDBC [and data are not written to disk], an explicit SHUTDOWN command is required [in order to keep data persistent]. In 1.8.0, a connection property, shutdown=true, can be specified on the first connection to the database (the connection that opens the database) to force a shutdown when the last connection closes.

So either send that SHUTDOWN command through a native query or add ;shutdown=true to the connection string:

jdbc:hsqldb:file:./database/database.hsql;shutdown=true

Regarding the question about write_delay, no, setting it to 0 won't cause a problem in a unit testing context (it "just" has an impact on performances). Actually, I would have expect shutdown=true to be enough as mentioned in the documentation:

Application Development and Testing

...

If you do not want to run a Server instance, and you need persistence between tests in different processes, then you should use a file: database. You can use the shutdown=true connection property to ensure the database is persisted fully after the connections are closed. An alternative option is to use hsqldb.write_delay=false connection property, but this is slightly slower than the other option.

It has been reported that some data access frameworks do not close all their connection to the database after the tests. In such situations, you need to use zero WRITE DELAY if you want the data to persist at the end of the tests

But I guess that you are facing the situation described in the second paragraph. I wonder if the data access layer is really guilty though.

Instead of calling em.getTransaction() twice, you need to call it once, and save the reference to it. Each time you call it, you get a new transaction. So you start one, but then commit a separate one.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!