Java 在spring事务中,数据未提交到DB
我的应用程序是spring、struts2和Hibernate,数据库是Postgress9.2。问题是DAO方法没有使用flush提交数据 My spring.xml:Java 在spring事务中,数据未提交到DB,java,spring,hibernate,spring-mvc,struts2,Java,Spring,Hibernate,Spring Mvc,Struts2,我的应用程序是spring、struts2和Hibernate,数据库是Postgress9.2。问题是DAO方法没有使用flush提交数据 My spring.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"
<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="dao" />
<jee:jndi-lookup id="depo" jndi-name="java:/depo"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="depo"/>
<property name="hibernateProperties">
<props>
<!-- <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> -->
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">update</prop> -->
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>model.AtOrganisation</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="orgdao" class="dao.OrganisationDaoImp">
<property name="sessionfactory" ref="sessionFactory" />
</bean>
<bean id="empAction" class="action.OraganisationAction">
<property name="orgdao" ref="orgdao" />
</bean>
谁能指出我错过了什么。
注意:如果我取消对flush()的注释,数据将被提交。首先在服务级别上使用@Transactional注释对方法进行注释,并保留DAO而不使用事务注释。然后让我们看看它是如何工作的。您实际上需要通过调用以下命令来检索Spring正在使用的相同会话:
Session session = sessionFactory.getCurrentSession();
调用
openSession()
将创建一个全新的会话,它不是由Spring管理的,与调用getCurrentSession()
相反,它检索由@Transactional
机制管理的相同会话。您有什么问题?数据没有插入数据库。我需要在不调用flush()的情况下执行此操作。为什么您认为在事务打开时刷新会话?是的,我不想使用flush。这就是为什么我需要您的帮助来删除刷新。当我注释刷新数据时,数据没有提交。我没有使用服务层。你们确定我是否添加了服务层来解决这个问题吗?当然我现在不确定,但我想是的。这就是我为什么写“让我们看看它是如何工作的”,我已经试过了。但是它说目前没有开放的会议。Session Session=sessionfactory.getCurrentSession();session.saveOrUpdate(org);好的,将spring.xml改为
Session session = sessionFactory.getCurrentSession();