我有一个名为NotificationService的实用程序服务。它沿着MailService的方向发展,服务层具有NotificationService接口(定义服务)和NotificationServiceUtil类(静态提供服务),并且我有一个提供NotificationServiceImpl(实现)的impl层。然后,我在ext-spring.xml中注册该服务:如何在没有服务构建器的情况下在liferay transactional中创建服务方法
<bean id="org.mitre.asias.portal.notification.service.NotificationService"
class="org.mitre.asias.portal.notification.service.impl.NotificationServiceImpl">
...
</bean>
<bean id="org.mitre.asias.portal.notification.service.NotificationServiceUtil"
class="org.mitre.asias.portal.notification.service.NotificationServiceUtil">
<property name="service"
ref="org.mitre.asias.portal.notification.service.NotificationService" />
</bean>
一切都按预期工作,直到我尝试将事务混合到一起。我在NotificationServiceImpl一个方法需要进行交易:
public void sendAndUpdate(Message message, List<Event> eventsToUpdate) throws SystemException, MessagingException {
for (Event event : eventsToUpdate) {
eventLocalService.updateEvent(event);
}
Transport.send(message);
}
的想法是,如果由于某种原因,消息发送失败,则更改模型对象将被回滚,所以我可以重试稍后发送。我尝试使用以下注释:
@Transactional(isolation = Isolation.PORTAL,
rollbackFor = { PortalException.class, SystemException.class, MessagingException.class })
但它没有采取。我尝试将该注释移动到NotificationService接口,但仍然没有骰子。我在org.hibernate.transaction.JDBCTransaction
上启用了调试日志记录,并且可以看到对该方法的调用从未启动事务,但启动并为循环中的每个updateEvent
调用提交了一个事务。
挖掘到源,看来Liferay的有一个bean后处理器称为ServiceBeanAutoProxyCreator
其中有这样的代码:
...
protected Object[] getAdvicesAndAdvisorsForBean(
Class<?> beanClass, String beanName, TargetSource targetSource)
throws BeansException {
if (beanName.endsWith(_SERVICE_SUFFIX)) {
return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS;
}
else {
return DO_NOT_PROXY;
}
}
private static final String _SERVICE_SUFFIX = "Service";
...
这使得它看起来好像每个bean的名字与服务(如我的豆确实结束:...id="org.mitre.asias.portal.notification.service.NotificationService"...
)应该被包裹在ServiceBeanAopProxy
这将根据服务构建器添加事务通知产生base-spring.xml
:
<bean class="com.liferay.portal.spring.aop.ServiceBeanAutoProxyCreator">
<property name="methodInterceptor" ref="serviceAdvice" />
</bean>
<bean class="com.liferay.portal.spring.context.PortletBeanFactoryCleaner" />
<bean class="com.liferay.portal.spring.context.PortletBeanFactoryPostProcessor" />
<bean class="com.liferay.portal.spring.bean.BeanReferenceAnnotationBeanPostProcessor" />
<bean id="portletClassLoader" class="com.liferay.portal.kernel.portlet.PortletClassLoaderUtil" factory-method="getClassLoader" />
<bean id="servletContextName" class="com.liferay.portal.kernel.portlet.PortletClassLoaderUtil" factory-method="getServletContextName" />
<bean id="basePersistence" abstract="true">
<property name="dataSource" ref="liferayDataSource" />
<property name="sessionFactory" ref="liferaySessionFactory" />
</bean>
<bean id="serviceAdvice" class="com.liferay.portal.monitoring.statistics.service.ServiceMonitorAdvice">
<property name="monitoringDestinationName" value="liferay/monitoring" />
<property name="nextMethodInterceptor" ref="asyncAdvice" />
</bean>
<bean id="asyncAdvice" class="com.liferay.portal.messaging.async.AsyncAdvice">
<property name="defaultDestinationName" value="liferay/async_service" />
<property name="nextMethodInterceptor" ref="threadLocalCacheAdvice" />
</bean>
<bean id="threadLocalCacheAdvice" class="com.liferay.portal.cache.ThreadLocalCacheAdvice">
<property name="nextMethodInterceptor" ref="transactionAdvice" />
</bean>
<bean id="transactionAdvice" class="com.liferay.portal.spring.transaction.TransactionInterceptor">
<property name="transactionAttributeSource" ref="transactionAttributeSource" />
<property name="transactionManager" ref="liferayTransactionManager" />
</bean>
<bean id="transactionAttributeSource" class="com.liferay.portal.spring.transaction.AnnotationTransactionAttributeSource" />
没有任何人有任何想法我怎么能做这个工作?
我在Tomcat 6.0.32容器中使用Liferay 6.0 EE sp2。