Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring@Transactional未提交记录_Spring_Hibernate_Spring Transactions - Fatal编程技术网

Spring@Transactional未提交记录

Spring@Transactional未提交记录,spring,hibernate,spring-transactions,Spring,Hibernate,Spring Transactions,我使用Spring和Hibernate。我正在使用以下配置。当我试图通过Spring事务保存时,记录从未提交 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.spri

我使用Spring和Hibernate。我正在使用以下配置。当我试图通过Spring事务保存时,记录从未提交

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd

    <context:component-scan base-package="com.wpt.controllers,com.wpt.dao" />

    <mvc:annotation-driven />

    <mvc:default-servlet-handler />

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="wptDatasource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/wp" />
        <property name="username" value="user" />
        <property name="password" value="pass" />
    </bean>
    <bean id="hibernate4AnnotatedSessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="wptDatasource" />
            <property name="packagesToScan" value="com.wpt.models" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.current_session_context_class">thread</prop>
                    <prop key="hibernate.show_sql">false</prop>
                </props>
            </property>
        </bean>

        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
            p:sessionFactory-ref="hibernate4AnnotatedSessionFactory">
        </bean></beans>
Mydao类

public void save(Item item){
  session = sf.openSession();
  session.save(item);
  session.close();
}
上述@Transactional配置未提交记录。但是,如果我从spring控制器中删除@Transactional并使用如下Hibernate事务,记录将提交

public void save(Item item){
  session = sf.openSession();
  Transaction tx = session.beginTransaction();
  session.persist(item);
  tx.commit();
  session.close();
}
此类提交记录。 我看过一些论坛,其中提到@Transactional将负责提交记录。我在这里犯了什么错误?有人能帮忙吗


提前感谢。

在我向配置xml添加了
注释并从HibernateSessionFactorybean中删除了
hibernate.current\u session\u context\u类之后,它就工作了。考虑到下面的变化

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.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">

<context:component-scan base-package="com.wpt.controllers,com.wpt.dao" />

<mvc:annotation-driven />

<mvc:default-servlet-handler />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="wptDatasource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/wp" />
    <property name="username" value="user" />
    <property name="password" value="pass" />
</bean>
<bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="wptDatasource" />
        <property name="packagesToScan" value="com.wpt.models" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="hibernate4AnnotatedSessionFactory">
    </bean>
</beans>

谢谢大家的帮助。

您有spring配置文件吗?是的,我已经在问题本身中给出了sessionfactory和transactionmanagement bean。我需要把整个文件放在这里吗?我的意思是引入一个带有事务方法的独立组件,控制器委托给它来执行业务逻辑。将@Transactional放在控制器上可能会导致问题,因为它引入了代理,并且控制器可以获取其他事物的代理,这可能会导致混乱。请尝试输入您的配置文件。这是启用使用注释的事务所必需的。此外,您还必须为txFirst添加架构位置,关闭所有您没有的
,这基本上使
@Transactional
无用。接下来,您将使用
hibernate.current\u session\u context\u类
,在您的例子中,它破坏了正确的Spring集成。去掉那个。另一件事是,让您的web层具有事务性不是一件好事(这就是您现在所做的)。创建服务并使其具有事务性。您不应该自己打开或关闭会话。您仍然没有使用spring,请使用
sf.getCurrentSession().save(item)
而不是现在使用的。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.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">

<context:component-scan base-package="com.wpt.controllers,com.wpt.dao" />

<mvc:annotation-driven />

<mvc:default-servlet-handler />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="wptDatasource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/wp" />
    <property name="username" value="user" />
    <property name="password" value="pass" />
</bean>
<bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="wptDatasource" />
        <property name="packagesToScan" value="com.wpt.models" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="hibernate4AnnotatedSessionFactory">
    </bean>
</beans>
public void save(Item item){
  //session = sf.openSession();
  session.save(item);
  //session.close();
}