我在Spring中遇到了这个事务。 我不知道为什么永远不会回滚。 有人知道它发生了什么吗?
的web.xml:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Security Application</display-name>
<!-- Spring MVC -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-database.xml, /WEB-INF/spring-security.xml</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
FileCabServiceImpl.java
@Service("FileCabService")
public class FileCabServiceImpl implements FileCabService
{
@Autowired
private FileCabMapper fileCabMapper;
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void insertFileCab(FileCab fileCab)
{
fileCabMapper.insertFileCab(fileCab);
throw new NullPointerException(); // Error to do the rollback
}
}
Controller.java:
@RestController
public class ReservationController
{
private final Logger logger = LoggerFactory.getLogger(ReservationController.class);
@Autowired
ReservationServiceImpl reservationService;
@Autowired
FileCabService fileCabService;
@Autowired
FileCounterService fileCounterService;
@RequestMapping(value = "/api/reservation", consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
public void reservationProcess()
{
FileCab fileCab = new FileCab();
fileCab.setnUser("user");
fileCab.setiPax(5);
fileCab.setnNamePax("");
Date dateIn = new Date();
fileCab.setdDateIn(dateIn);
fileCab.setdDateOut(dateIn);
fileCab.setnCodeRef("");
int fileCounter = fileCounterService.getiFileCounterByYear(2017, 1);
fileCab.setnTravelFile(String.valueOf(++fileCounter));
fileCab.setiUserGroup(1);
fileCab.setnYear(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
fileCab.setdOpenDay(Calendar.getInstance().getTime());
fileCab.setiAgency(0);
fileCab.setnStatus("Cerrado");
fileCab.setbPrePaid(0);
fileCab.setbPaid(0);
fileCab.setdDatePrePaid(null);
fileCab.setDcPrePaidAmount(BigDecimal.ZERO);
fileCab.setiTourService(0);
fileCab.setnObservations("");
fileCabService.insertFileCab(fileCab);
}
}
弹簧database.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName">
<context:annotation-config />
<context:component-scan base-package="es.app.spring" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://xxx.xxx.xxxx.xxx:3306/spring_res?autoReconnect=true" />
<property name="username" value="XXXXXX" />
<property name="password" value="XXXXXX" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="es.app.spring.model"/>
<property name="mapperLocations" value="classpath*:es.app.spring.mappers/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="es.app.spring.mappers" />
</bean>
</beans>
任何帮助都将不胜感激。 谢谢