<jpa:repositories base-package="com.apress.prospring3.ch13.repository"
entity-manager-factory-ref="emf"
transaction-manager-ref="transactionManager"/>
</beans>
As shown in Listing 13-21, the AOP transaction advice was removed. In addition, a
transactionTemplate bean was defined using the
org.springframework.transaction.support.TransactionTemplate class, with the transaction attributes
defined in the properties section. Let's take a look on the implementation of the countAll() method,
which is shown in Listing 13-22.
Listing 13-22. Programmatic Transaction Management Implementation
package com.apress.prospring3.ch13.service.programmatic;
// Import statements omitted
@Service("contactService")
@Repository
public class ContactServiceImpl implements ContactService {
@Autowired
private TransactionTemplate transactionTemplate;
@PersistenceContext
private EntityManager em;
// Other methods not implemented
public long countAll() {
return transactionTemplate.execute(new TransactionCallback<Long>() {
public Long doInTransaction(TransactionStatus transactionStatus) {
return em.createNamedQuery("Contact.countAll",
Long.class).getSingleResult();
}
});
}
}
In Listing 13-22, the TransactionTemplate class was injected from Spring. And then in the
countAll() method, the TransactionTemplate.execute() method was invoked, passing in a declaration
of an inner class that implements the TransactionCallback<T> interface. Then the doInTransaction()
was overridden with the desired logic. The logic will run within the attributes as defined by the
transactionTemplate bean. Listing 13-23 shows the testing program.
Listing 13-23. Testing Programmatic Transaction
package com.apress.prospring3.ch13;
import org.springframework.context.support.GenericXmlApplicationContext;
import com.apress.prospring3.ch13.service.ContactService;
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home