views:

286

answers:

2

It is possible to use the Spring Framework's @Transactional support outside of a Spring container. In reference documentation is chapter about AspectJ aspect. I'm trying to use it in my wicket application, but with no positive result.

application-context.xml:

<tx:annotation-driven transaction-manager="transactionManager" mode="aspectj" />
<context:annotation-config />
<context:component-scan base-package="com.wicket.app"/>
<context:spring-configured />

<bean id="annotationTransactionAspect" factory-method="aspectOf"
      class="org.springframework.transaction.aspectj.AnnotationTransactionAspect">
    <property name="transactionManager" ref="transactionManager"></property>
</bean>

In my form class annotated by @Configurable, I have:

@Transactional
public void process(IFormSubmittingComponent submittingComponent) {
    super.process(submittingComponent);
    getDao().getEntityManager().flush();
}

Stack trace:

org.apache.openjpa.persistence.TransactionRequiredException: Can only perform operation while a transaction is active.
+1  A: 

You might be able to get this working using AspectJ load-time-weaving, but that's a very complex solution for a simple problem.

If you need declarative transactions, then I suggest you move the transactional logic from the wicket component down into a Spring bean, and invoke the Spring bean from the wicket object. The Spring bean would have the transactional annotations, and would be proxied correctly by the Spring container.

skaffman
Yeah, my fault. This simple solution is better. There is no need to exaggerate.
nablik
A: 

I have no experience with Wicket. But is your 'form class' (the one that contains method annotated with @Transactional) Spring managed code? i.e. Who creates the instances of the class?

If it's not, that Spring will not provide @Transactional support (neither will @Autowired work, etc).

Grzegorz Oledzki
@Transactional may be used outside Spring container, for example on entity class <a href=http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative-aspectj>documentation</a>. But I did something wrong, and it doesn't work.
nablik
Are you refering to http://static.springsource.org/spring/docs/2.0.x/reference/transaction.html#transaction-declarative-aspectj. Then yes - you are right, it would add `@Transactional` support for non-Spring managed code, but all of these classes still have to undergo some kind of processing by Spring (what they call `spring-aspects.jar`).
Grzegorz Oledzki
I'm trying to add aspectj processing, but it doesn't work. Any sugestions why?
nablik