Java 用hibernate实现嵌入式数据库的慢速处理

Java 用hibernate实现嵌入式数据库的慢速处理,java,hibernate,orm,h2,embedded-database,Java,Hibernate,Orm,H2,Embedded Database,我在嵌入式模式下使用h2数据库,也使用hibernate访问它。这是我用来初始化hibernate的spring配置: <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSour

我在嵌入式模式下使用h2数据库,也使用hibernate访问它。这是我用来初始化hibernate的spring配置:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>

org.hibernate.dial.h2方言
假的
更新
类名1
类名2
类名3
当我尝试插入数据时,数据库工作非常非常慢,我有非常大的IO流(和硬盘驱动器一样快)。我认为每次getHibernateTemplate()保存(问题)时,数据库都是打开和关闭的;被称为。有趣的是,若我将连接字符串更改为使用独立服务器,那个么这个问题就会消失,并且一切正常

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>
getHibernateTemplate().save(question); 我的嵌入式数据库配置有什么问题

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>
UPD

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.jdbcx.JdbcDataSource" />
    <property name="url" value="jdbc:h2:file:C:\temp\data.db" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

只要您请求连接,该实现就会打开和关闭连接

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>
从JavaDoc:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>
注意:这个类不是一个实际的类 连接池;事实上并不是这样 池连接。它只是作为 简单地更换一个完整的 连接池,实现相同的功能 标准界面,但创建新的 每通电话都有连接

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>

对于生产环境,强烈建议您使用一个连接池,该连接池可以预先打开有限数量的连接,并将这些连接池用于使用。对于您的
数据源
,我建议使用或。显示如何设置数据源以使用DBCP。使用连接池肯定会减少查询数据库的时间,也肯定会解决您的问题。

您的
数据源是什么样子的?你使用连接池吗?配置参数是什么?@benjamin muschko我已经为数据源添加了配置,你添加了
;数据库URL的DB_CLOSE_DELAY=1
?我不太了解Spring,但是如果它是一个
DriverManager数据源
,为什么不直接使用
org.h2.Driver
?谢谢你的DB\u CLOSE\u延迟,我会尝试一下并在这里写下结果。我认为org.h2.jdbcx.JdbcDataSource在内部使用org.h2.Driver,所以差别不大。我添加了DB_CLOSE_DELAY=-1,不再有不必要的打开和关闭
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource">
        <ref bean="dataSource" />
    </property>

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>

    <property name="annotatedClasses">
        <list>
            <value>classname1</value>
            <value>classname2</value>
            <value>classname3</value>
        </list>
    </property>
</bean>