使用 hibernate 进行缓慢的嵌入式数据库处理

发布于 2024-11-17 03:30:49 字数 1702 浏览 5 评论 0原文

我在嵌入式模式下使用 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>

当我尝试插入数据时,数据库工作非常非常慢,而且我有非常大的 IO 流(与硬盘驱动器一样快)。我认为数据库每次都会打开和关闭

getHibernateTemplate().save(question);
is called. What is interesting, if I change connection string to use standalone server this problem disappears and all works nice.

我的嵌入式数据库配置有什么问题?

UPD

<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>

I use h2 database in embedded mode, also I use hibernate to access it. That is my spring config I am using to initialize hibernate:

<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>

when I try insert data database works very, very slow and I have very huge IO stream (as fast as hard drive is able). I think database is opened and closed every time when

getHibernateTemplate().save(question);

is called. What is interesting, if I change connection string to use standalone server this problem disappears and all works nice.

What is wrong with my config for embedded databases?

UPD

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小帐篷 2024-11-24 03:30:49

实现 org.springframework.jdbc每当您请求连接时,.datasource.DriverManagerDataSource 都会打开和关闭连接。

来自 JavaDoc:

注意:这个类不是实际的
连接池;它实际上并不
池连接。它只是充当
简单替代成熟的
连接池,实现相同
标准接口,但创造新的
每次通话均保持连接。

对于生产环境,强烈建议您使用连接池,它预先打开有限数量的连接并将它们集中起来供使用。对于您的 dataSource 我建议使用 Apache 的 Jakarta Commons DBCPC3P0此示例展示了如何设置数据源< /code> 使用 DBCP。使用连接池肯定会减少查询数据库所花费的时间,并且肯定会解决您的问题。

The implementation org.springframework.jdbc.datasource.DriverManagerDataSource opens and closes a connection whenever you request one.

From the JavaDoc:

NOTE: This class is not an actual
connection pool; it does not actually
pool Connections. It just serves as
simple replacement for a full-blown
connection pool, implementing the same
standard interface, but creating new
Connections on every call.

For production environments it's highly recommended that you use a connection pool which opens a limited number of connections up-front and pools them for usage. For your dataSource I'd say use Apache's Jakarta Commons DBCP or C3P0. This example shows how you can set up your dataSource to use DBCP. Using a connection pool will definitely reduce the time spend to query the database and will most certainly solve your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文