0

I'm working with Hibernate. How can I configure my applicationContext.xml to have an H2 in-memory databaseorg.hibernate.dialect.H2Dialect dont work

Spring configuration

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="org.h2.Driver" /> <property name="url" value="jdbc:h2:tcp://localhost/~/test" /> <property name="username" value="sa" /> <property name="password " value="" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.emusicstore</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 

1
  • 1
    Please, can you give more information what is the error message? Commented Sep 18, 2018 at 11:32

2 Answers 2

2

You are not connecting to an in-memory database. Your JDBC URL is for network connection to localhost:

jdbc:h2:tcp://localhost/~/test 

To use the in-memory H2 the URL must look like this, containing mem:

jdbc:h2:mem:testdb 

In the manual, see the section on In-Memory Database

Sign up to request clarification or add additional context in comments.

1 Comment

Also, a tip: Be aware that the in-memory database is discarded, by default, when the connection closes. You can override this behavior. See: Keep H2 in-memory database between connections and H2 in-memory database. Table not found
0

Use embedded db datasource (with dbcp connection pool) instead of drivermanager datasource.

 <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="classpath:db/sql/create-db.sql" /> <jdbc:script location="classpath:db/sql/insert-data.sql" /> </jdbc:embedded-database> <bean id="dbcpDataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <property name="driverClassName" value="org.h2.Driver" /> <property name="url" value="jdbc:hsqldb:mem:dataSource" /> <property name="username" value="username" /> <property name="password" value="password" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" > <property name="dataSource" ref="dbcpDataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.emusicstore</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> 

1 Comment

share stacktrace !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.