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>

m.h.1994
  • 1
  • 2

2 Answers2

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • 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*](https://dba.stackexchange.com/q/224338/19079) and [*H2 in-memory database. Table not found*](https://stackoverflow.com/q/5763747/642706) – Basil Bourque Dec 06 '18 at 21:26
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>
Seymur Asadov
  • 612
  • 5
  • 19