[英]Why configure both dataSource and sessionFactory in Spring-Hibernate Configuration?
I'm using Spring 3.1.2 and Hibernate 4.1.7 for my web application. 我在我的Web应用程序中使用Spring 3.1.2和Hibernate 4.1.7。 I want to now configure both of these. 我想现在配置这两个。 I have my hibernate.cfg.xml
file: 我有我的hibernate.cfg.xml
文件:
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!--
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
-->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
My webapp-servlet.xml
spring config file: 我的webapp-servlet.xml
spring配置文件:
<beans>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
<property name = "dataSource" ref = "dataSource"></property>
</bean>
<bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource">
<property name = "driverClassName" value = "com.mysql.jdbc.Driver" />
<property name = "url" value = "jdbc:mysql://localhost:3306/test" />
<property name = "username" value = "root" />
<property name = "password" value = "root" />
<property name = "maxActive" value = "10" />
</bean>
</beans>
DataSource
s I can use? 我可以使用哪些其他DataSource
? You don't need both of them. 你不需要他们两个。 You can either get rid of hibernate.cfg.xml
and configure everything in LocalSessionFactoryBean
, or reuse your existing hibernate.cfg.xml
as is (in this case you don't need to configure DataSource
in Spring config). 您可以删除hibernate.cfg.xml
并在LocalSessionFactoryBean
配置所有LocalSessionFactoryBean
,或者按LocalSessionFactoryBean
重用现有的hibernate.cfg.xml
(在这种情况下,您不需要在Spring配置中配置DataSource
)。
You have the following options: 您有以下选择:
Use embedded database - it's good for testing and learning purposes 使用嵌入式数据库 - 它有利于测试和学习目的
Use DriverManagerDataSource
- it's a simple non-pooled datasource that can be used for testing, etc (not recommended for production use) 使用DriverManagerDataSource
- 它是一个简单的非池化数据源,可用于测试等(不推荐用于生产用途)
Use connection pool such as DBCP or c3p0 使用连接池,如DBCP或c3p0
If you deploy to application server you can use connection pool provided by the application server using JNDI 如果部署到应用程序服务器,则可以使用应用程序服务器使用JNDI提供的连接池
Your current configuration is sufficient, but it lacks support of Spring transaction management . 您当前的配置已足够,但它缺乏对Spring事务管理的支持。 In order to enable it you need to 为了实现它,你需要
Declare HibernateTransactionManager
声明HibernateTransactionManager
Add <tx:annotation-driven>
to enable declarative transaction management ( @Transactional
) 添加<tx:annotation-driven>
以启用声明式事务管理( @Transactional
)
Declare TransactionTemplate
if you want to use programmatic transaction management (use it to overcome limitations of declarative transaction management) 声明TransactionTemplate
,如果你想使用编程式事务管理(用它来克服声明式事务管理的限制)
Also don't forget to remove transaction-related properties from Hibernate configuration since they may conflict with Spring transaction management 另外,不要忘记从Hibernate配置中删除与事务相关的属性,因为它们可能与Spring事务管理冲突
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.