简体   繁体   English

为hibernate和@Transactional配置spring数据源

[英]Configure spring datasource for hibernate and @Transactional

At this moment I'm using DriverManagerDataSource with @Transactional annotation to manage transactions. 此时我正在使用带有@Transactional注释的DriverManagerDataSource来管理事务。 But all transactions are very very slow, probably because data source open and close connection to db each time. 但是所有事务都非常慢,可能是因为数据源每次都打开和关闭与db的连接。

What data source should I use to speed up transaction? 我应该使用什么数据源来加速交易?

I am using in my application combination of two approaches. 我在我的应用程序中使用两种方法的组合。 the first one is c3p0 connection pooling, its almost the same solution as chkal sugested. 第一个是c3p0连接池,它与chkal sugested几乎相同的解决方案。 The second approach is to use Spring lazyConnectionDataSourceProxy, which creates lazy loading proxy that loads connection only if you hit the database. 第二种方法是使用Spring lazyConnectionDataSourceProxy,它创建延迟加载代理,只有在您访问数据库时才加载连接。 This is very useful, when you have second level cache and you are only reading cached data and queries - database wont be hit, and you don't need to acquire connection (which is pretty expensive). 这是非常有用的,当你有二级缓存而你只是阅读缓存数据和查询 - 数据库不会被命中,你不需要获取连接(这是非常昂贵的)。

<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driverClassName}" />
    <property name="jdbcUrl" value="${jdbc.url}" />
    <property name="user" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <!-- Pool properties -->
    <property name="minPoolSize" value="5" />
    <property name="initialPoolSize" value="10" />
    <property name="maxPoolSize" value="50" />
    <property name="maxStatements" value="50" />
    <property name="idleConnectionTestPeriod" value="120" />
    <property name="maxIdleTime" value="1200" />

</bean>

<bean name="lazyConnectionDataSourceProxy" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
    <property name="targetDataSource" ref="dataSource" />
</bean>

DriverManagerDataSource isn't actually a connection pool and should only be used for testing. DriverManagerDataSource实际上不是连接池,只应用于测试。 You should try BasicDataSource from Apache Commons DBCP . 您应该从Apache Commons DBCP尝试BasicDataSource Something like: 就像是:

<bean id="dataSource" destroy-method="close" 
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM