org.h2.jdbc.JdbcSQLException: Database is already closed when autowiring UiUtils in context sensitive test

Hi @dkayiwa,

In the Address Hierarchy module, as soon as I autowire UiUtils in AddressHierarchyAjaxControllerTest I get this

ERROR - SchemaExport.execute(287) |2017-03-18 11:15:35,416| schema export unsuccessful
org.h2.jdbc.JdbcSQLException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-135]

Full stack trace here.

I thought this could be related to the Spring version but it also fails the same way when running against Core 2.0.x. I added a new Maven build profile ‘openmrs-2.0’ for this, should you need to try stuff against Core 2.x (mvn -P openmrs-2.0 clean install).

Would you mind running a mvn clean install on your side? You can clone commit 86755eb2 from our fork to reproduce it.


I just don’t know yet enough of all the plumbing behind context sensitive tests to take action here. Where is the DB URL defined for the unit tests for instance, or how to overwrite it as suggested in the error message?

Thanks in advance for shedding light!

@mksd, I’m not seeing that error in the full stack trace you provided. I do this this line in there, if it helps:

ERROR - AddressHierarchyServiceImpl.getPossibleAddressValues(82) |2017-03-18 12:02:45,362| Address field ADDRESS_1 is not mapped to address hierarchy level.

Mike

Thanks @mseaton for looking into it. You can actually view the full Maven build stack trace here: https://gist.githubusercontent.com/mks-d/ecf9b056bdae8184a52ac0a88368d241/raw/e0150f66214b3b01f39027f2645715c7bcb375a7/gistfile1.txt

The error pops up at the very end:

ERROR - SchemaExport.execute(287) |2017-03-18 12:02:50,801| schema export unsuccessful
org.h2.jdbc.JdbcSQLException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-135]
 at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
 at org.h2.message.DbException.get(DbException.java:167)
 at org.h2.message.DbException.get(DbException.java:144)
 at org.h2.message.DbException.get(DbException.java:133)
 at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1348)
 at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1323)
 at org.h2.jdbc.JdbcConnection.getAutoCommit(JdbcConnection.java:386)
 at com.mchange.v2.c3p0.impl.NewProxyConnection.getAutoCommit(NewProxyConnection.java:985)
 at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:82)
 at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
 at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:263)
 at org.hibernate.tool.hbm2ddl.SchemaExport.drop(SchemaExport.java:229)
 at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:961)
 at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.destroy(AbstractSessionFactoryBean.java:251)
 at org.springframework.orm.hibernate3.LocalSessionFactoryBean.destroy(LocalSessionFactoryBean.java:899)
 at org.openmrs.api.db.hibernate.HibernateSessionFactoryBean.destroy(HibernateSessionFactoryBean.java:196)
 at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:184)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:487)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:463)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:431)
 at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1048)
 at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1022)
 at org.springframework.context.support.AbstractApplicationContext$3.run(AbstractApplicationContext.java:940)

I realise it was not practical to point to a commit URL. If you have a few mins, you could clone this branch: https://github.com/mekomsolutions/openmrs-module-addresshierarchy/tree/MRSCMNS-28 I really haven’t done much so far, just trying to autowire UiUtils in the existing unit test. If you comment out these three lines everything gets back to normal:

@Autowired
@Qualifier("uiUtils")
private UiUtils ui;

The database exception is just a side effect of another failure which is the real cause. That is: NoSuchBeanDefinitionException: No matching bean of type [org.openmrs.ui.framework.UiUtils] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

Since UiUtils is not a spring bean itself, you would need a spring bean that sub classes it for such to work.

Regardless of the solution of getting this to work, I don’t actually think wiring in UiUtils is the right solution to this problem. As I think @mogoodrich replied in the other thread - we don’t need to depend on the uiframework in order to support localization:

Translation support is already in the API, it is not limited to the Web (I’m not sure if this was your concern @rubailly, but there is nothing wrong with doing translations in the API project). It may be that UiUtils adds some functionality around this, but this should be simple to reproduce.

As far as I know, addresshierarchy does not depend on uiframework or related modules. It would be better to keep it that way, if possible.

Mike

1 Like

Sure MessageSourceService will be used directly (and my initial question becomes irrelevant).

Hello , Am working on this ticket ,After cloning metadatasharing module, i encountered error logs here which is in the same thread, but unfortunately it seems the correct solution was not clear to me. Am still not in better position to check out how h2 database was defined in MDS module cc @mksd @dkayiwa @mseaton

Am facing the same error logs but this time round, am running metadata sharing module. Tried to make some changes by configuring the h2 database but the same error pops up. how did you came along this thanks cc @mksd

How can this be reproduced? I don’t understand what the ticket is about, bugs should specify clear steps for reproduction. Can you add them?

Sorry, but i went ahead and cloned openmrs-module-metadatasharing module from git, and run mvn clean install. without any new commits then i encountered the same logs thanks

Ok I see, you see this on a couple of Spring tests:

org.h2.jdbc.JdbcSQLException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-135]
  at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
  at org.h2.message.DbException.get(DbException.java:167)
  at org.h2.message.DbException.get(DbException.java:144)
  at org.h2.message.DbException.get(DbException.java:133)
  at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1348)
  at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1323)
  at org.h2.jdbc.JdbcConnection.getAutoCommit(JdbcConnection.java:386)
  at com.mchange.v2.c3p0.impl.NewProxyConnection.getAutoCommit(NewProxyConnection.java:985)
  at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:82)
  at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
  at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:263)
  at org.hibernate.tool.hbm2ddl.SchemaExport.drop(SchemaExport.java:229)
  at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:961)
  at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.destroy(AbstractSessionFactoryBean.java:251)
  at org.springframework.orm.hibernate3.LocalSessionFactoryBean.destroy(LocalSessionFactoryBean.java:899)
  at org.openmrs.api.db.hibernate.HibernateSessionFactoryBean.destroy(HibernateSessionFactoryBean.java:192)

But the overall build is not failing.

You can try what is suggested:

to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL

Here is an example from elsewhere (credits to @long27km):

@Override
public Properties getRuntimeProperties() {
  Properties props = super.getRuntimeProperties();
  
  String dbUrl = props.getProperty(Environment.URL);
  dbUrl += ";DB_CLOSE_ON_EXIT=FALSE";
  props.setProperty(Environment.URL, dbUrl);
  
  return props;
}
1 Like

Thanks alot, let me try that