Spring @Transactional 未提交记录
Spring @Transactional not committing records
我在 Hibernate 中使用 Spring。我正在使用以下配置。当我尝试通过 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>
我的控制器类
@Transactional
@RequestMapping(...)
public String saveRecords(@ModelAttribute("orderObj") Order order){
for(Item item : order.getItems()){
itemDAO.save(item);
}
return "saveSuccess";
}
MyDAOClass
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();
}
这class 提交记录。
我浏览了一些论坛,其中提到@Transactional 将负责提交记录。我在这里犯了什么错误?有人可以帮忙吗?
提前致谢。
在我将 <tx:annotation-driven />
注释添加到我的配置 xml 并从 hibernateSessionFactory bean 中删除 hibernate.current_session_context_class
之后它起作用了。鉴于以下变化。
<?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>
然后我在我的所有服务方法中包含了 org.springframework.transaction.annotation.Transactional
注释并从我的 DAO 类 中删除了所有事务。
public void save(Item item){
//session = sf.openSession();
session.save(item);
//session.close();
}
感谢大家的帮助。
我在 Hibernate 中使用 Spring。我正在使用以下配置。当我尝试通过 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>
我的控制器类
@Transactional
@RequestMapping(...)
public String saveRecords(@ModelAttribute("orderObj") Order order){
for(Item item : order.getItems()){
itemDAO.save(item);
}
return "saveSuccess";
}
MyDAOClass
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();
}
这class 提交记录。 我浏览了一些论坛,其中提到@Transactional 将负责提交记录。我在这里犯了什么错误?有人可以帮忙吗?
提前致谢。
在我将 <tx:annotation-driven />
注释添加到我的配置 xml 并从 hibernateSessionFactory bean 中删除 hibernate.current_session_context_class
之后它起作用了。鉴于以下变化。
<?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>
然后我在我的所有服务方法中包含了 org.springframework.transaction.annotation.Transactional
注释并从我的 DAO 类 中删除了所有事务。
public void save(Item item){
//session = sf.openSession();
session.save(item);
//session.close();
}
感谢大家的帮助。