它涉及在单个事务中的两个表中的插入操作。如有异常 交易没有回滚。事务在第一个事务中启动,下一个dao调用在同一个会话中继续。回滚异常模式已设置,仍然没有回滚。任何人都可以请帮助 Pom依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.9.Final</version>
</dependency>
</dependencies>
Confiuration xml
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="com.byteslounge.spring.tx.dao.impl" />
<context:component-scan base-package="com.byteslounge.spring.tx.user.impl" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@org-e0a6b74ade3:1521:XE" />
<property name="username" value="system" />
<property name="password" value="password" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
</props>
</property>
<property name="packagesToScan" value="com.byteslounge.spring.tx.model" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory">
<property name="nestedTransactionAllowed" value="true" />
</bean>
</beans>
道:
@Repository
public class AddressDaoImpl implements AddressDao{
@Autowired
private SessionFactory sessionFactory;
@Transactional(propagation=Propagation.MANDATORY ,
rollbackFor={MyEx1.class,Exception.class,org.hibernate.HibernateException.class})
public void insertAddress(Address address) {
Session session = sessionFactory.getCurrentSession();
System.out.println("session>>>>>>>>>>>>>>>>>>>>>>>>>>3* "+ session.hashCode());
session.save(address);
session.flush();
}
}
@Transactional(propagation=Propagation.REQUIRES_NEW,
rollbackFor={MyEx1.class,Exception.class,org.hibernate.HibernateException.class})
public void insertUser(User user, Address address) throws MyEx1 {
Session session = sessionFactory.getCurrentSession();
System.out.println("session>>>>>>>>>>>>>>>>>>>>>>>>>>2* "+ session.hashCode());
session.save(user);
addressDao.insertAddress(address);
session.flush();
session.close();
throw new MyEx1("User inserted!");
//session.getTransaction().commit();
//session.close();
}