您好,我在 Spring 上设置 hibernate 时遇到问题。我能够使其工作,但它在数据库上创建了大量 session 。据我所知,它为 spring.xml 上的每个 bean 创建 session 。由于我声明了 6 个 bean,因此在应用程序启动时我在数据库上也有 6 个 session ,这是我的代码
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
>
<!-- Uncomment and add your base-package here: <context:component-scan base-package="org.springframework.samples.service"/> -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:" />
<property name="username" value="Use" />
<property name="password" value="Pass" />
</bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<!-- <prop key="hibernate.current_session_context_class">managed</prop> -->
<prop key="hibernate.show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
</bean>
<bean id="PDao" class="com.PDaoImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="PService" class="com.PServiceImpl">
<property name="pDao" ref="PDao" />
</bean>
<bean id="MNDao" class="com.MNDaoImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="MNService" class="com.MNServiceImpl">
<property name="MNDao" ref="MNDao" />
</bean>
<bean id="SWDao" class="com.SWDaoImpl">
<property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
</bean>
<bean id="SWService" class="com.SWServiceImpl">
<property name="SWDao" ref="SWDao" />
</bean>
最佳答案
您需要使用 transactionManager 来为您管理 session 。
将以下代码行添加到您的 spring.xml
....
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="yourSessionFactory" />
</bean>
....
然后你必须在你的服务实现类上注释 @Transactional("transactionManager") 以使 transactionManager 通过 session 管理事务
@Transactional("transactionManager")
public class PServiceImpl implements PServiceImpl{
....
只是一个建议,您可以通过注释替换 DI 的 XML 配置,以使其变得简单
在 spring.xml 中,删除所有 beans 声明(xxservice 和 xxxdao)并将其替换为: <context:component-scan base-package="put here the package where your services and daos are lacated" />
您的服务必须如下所示:
@Service
@Transactional("transactionManager")
public class XXXServiceImpl implements XXXService{
@Autowired
private XXXDAO xxxDAO;
...
}
你的 dao 必须看起来像:
@Repository
public class XXXDAOImpl implements XXXDAO {
@Autowired
private SessionFactory sessionFactory;
...
}
还有一件事,在文件配置 header 中添加 tx 架构,您的 spring.xml 应该如下所示:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
关于java - Spring Hibernate session 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191350/