背景/概念:
我试图通过使用Hibernate(5.0.5.Final)+ Spring(4.2.3.RELEASE)将数据保存到Mysql数据库。
DAO:
@Repository
@Transactional(propagation = Propagation.REQUIRED,rollbackFor =
{Exception.class})
public abstract class DaoImpl implements Dao {
@Autowired
private SessionFactory sessionFactory;
@Override
public <T extends Entity> void saveOrUpdate(T entity) {
if (entity.getId() == null || entity.getId() == 0) {
create(entity);
} else {
update(entity);
}
}
服务:
@Service("entityService")
public class EntityServiceImpl implements EntityService {
.....
}
Tx配置:
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Spring/Hibernate TransactionManager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
<property name="dataSource" ref="mainDataSource" />
</bean>
<!-- the transactional advice -->
<tx:advice id="defaultTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" propagation="SUPPORTS" isolation="DEFAULT"
read-only="true"/>
<tx:method name="find*" propagation="SUPPORTS" isolation="DEFAULT"
read-only="true"/>
<tx:method name="list*" propagation="SUPPORTS" isolation="DEFAULT"
read-only="true"/>
<tx:method name="is*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
<tx:method name="check*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="change*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="modify*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="servicesConfig"
expression="execution(* com.example.service..*Service.*(..))"/>
<aop:advisor pointcut-ref="servicesConfig" advice-ref="defaultTxAdvice"/>
</aop:config>
Web xml:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
问题:
单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-main-context.xml")
public class TestService {
@Autowired
private UserHistoryService entityService;
@Test
public void testSaveNewKeyword(){
entityService.saveKeyWord("SRD");
entityService.updateScroing("text", "SRD");
}
}
可以将数据更新到数据库的控制器代码
@Controller
public class SearchController {
@Autowired
private UserHistoryService entityService;
@RequestMapping(value = "/rd/search/result/{base}/{page}")
public String rdSearchResult(Model model, @PathVariable("base") String base,
@PathVariable("page") int page,
@ModelAttribute("searchDataModel") SearchDataModel searchDataModel)
{
entityService.saveKeyWord("SRD");
entityService.updateScroing("text", "SRD");
}
}
我试图调试并观察这两者之间的差异。 我在Junit中运行测试我看到了Transaction Interceptor被调用。 但我运行Web应用程序,我没有看到事务拦截器调用。