使用 hibernate 进行缓慢的嵌入式数据库处理
我在嵌入式模式下使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现 org.springframework.jdbc每当您请求连接时,.datasource.DriverManagerDataSource 都会打开和关闭连接。
来自 JavaDoc:
对于生产环境,强烈建议您使用连接池,它预先打开有限数量的连接并将它们集中起来供使用。对于您的
dataSource
我建议使用 Apache 的 Jakarta Commons DBCP 或 C3P0。 此示例展示了如何设置数据源< /code> 使用 DBCP。使用连接池肯定会减少查询数据库所花费的时间,并且肯定会解决您的问题。
The implementation org.springframework.jdbc.datasource.DriverManagerDataSource opens and closes a connection whenever you request one.
From the JavaDoc:
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 yourdataSource
to use DBCP. Using a connection pool will definitely reduce the time spend to query the database and will most certainly solve your problem.