hibernate 4和spring 3配置问题

时间:2012-07-23 11:41:48

标签: spring hibernate

我在春天3使用hibernate 4,我在xml中配置它:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="propertiesConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/conf/jdbc.properties</value>
            </list>
        </property>
</bean>



    <tx:annotation-driven transaction-manager="transactionManager"/>
    <context:annotation-config />
    <context:component-scan base-package="com.friendsalert"/>
    <aop:aspectj-autoproxy/>

    <!--    
<bean id="log4jInitialization"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"    >
    <property name="targetClass"
        value="org.springframework.util.Log4jConfigurer" />
    <property name="targetMethod" value="initLogging" />
    <property name="arguments">
        <list>
            <value>/WEB-INF/conf/log4j.xml</value>
             this value is bad for production.
            <value>10000</value>

        </list>
    </property>
</bean>    
 -->
<!-- ========================= RESOURCE DEFINITIONS ========================= -->

<!-- Local Apache Commons DBCP DataSource that refers to a combined database -->
<!-- (see dataAccessContext-jta.xml for an alternative) -->
<!-- The placeholders are resolved from jdbc.properties through -->
<!-- the PropertyPlaceholderConfigurer in applicationContext.xml-->

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

</bean>

<!-- Transaction manager for a single JDBC DataSource -->
<!-- (see dataAccessContext-jta.xml for an alternative) 
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="localSessionFactory"/>
</bean>


<bean id="localSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" depends-on="dataSource">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
            <value>com/friendsalert/model/User.hbm.xml</value>

        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <!-- prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop -->
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.cache.provider_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
            <prop key="hibernate.cache.use_query_cache">true</prop> 

                <!--  cache factory for hibernate 3.3 (surrently we use 3.2)-->
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>                              
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.charSet">UTF8</prop>
            <!--  <prop key="hibernate.current_session_context_class">thread</prop>-->
            <prop key="hibernate.connection.aggressive_release">false</prop>                    
            <prop key="hibernate.connection.release_mode">after_transaction</prop>  
            <prop key="hibernate.connection.autocommit">true</prop>
            <!--  create / drop.. use exporter class instead!-->
            <prop key="hibernate.hbm2ddl.auto">update</prop> 
        </props>
    </property>
</bean>

<bean id="userDao" class="com.friendsalert.dao.UserDao">
<property name="sessionFactory" ref="localSessionFactory"/>     
 </bean>



</beans>

我用dao。当我进行交易(保存,获取,更新)时,它在第一次工作,第二次我收到交易关闭的错误。 我在我的dao中为每个函数使用@Transactional注释,所以我无法找出没有事务的原因。

任何人都可以告诉我我做错了什么吗?

感谢

1 个答案:

答案 0 :(得分:1)

您配置的不寻常之处是:

<prop key="hibernate.connection.aggressive_release">false</prop>                    
<prop key="hibernate.connection.release_mode">after_transaction</prop>  
<prop key="hibernate.connection.autocommit">true</prop>

我会尝试没有那些线。

从参考文档:不建议使用自动提交模式 hibernate.connection.autocommit
为JDBC池连接启用自动提交(不建议这样做)。 例如是的|假 hibernate.connection.release_mode指定Hibernate何时应该释放JDBC连接。默认情况下,将保持JDBC连接,直到会话显式关闭或断开连接。对于应用程序服务器JTA数据源,使用after_statement在每次JDBC调用后积极地释放连接。对于非JTA连接,通常使用after_transaction在每个事务结束时释放连接是有意义的。 auto将为JTA和CMT事务策略选择after_statement,为JDBC事务选择after_transaction