ctx.load("classpath:tx-annotation-app-context.xml");
ctx.refresh();
ContactService contactService = ctx.getBean("contactService",
ContactService.class);
// Testing countAll() method
System.out.println("Contact count: " + contactService.countAll());
}
}
Running the program will produce the following output:
DEBUG [org.springframework.orm.jpa.JpaTransactionManager] ­
<Creating new transaction with name
[com.apress.prospring3.ch13.service.annotation.ContactServiceImpl.countAll]:
PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly; ''>
DEBUG [org.hibernate.transaction.JDBCTransaction] - <begin>
Hibernate: select count(*) as col_0_0_ from contact contact0_ limit ?
DEBUG [org.hibernate.transaction.JDBCTransaction] - <commit>
DEBUG [org.hibernate.transaction.JDBCTransaction] - <re-enabling autocommit>
DEBUG [org.hibernate.transaction.JDBCTransaction] - <committed JDBC Connection>
DEBUG [org.springframework.orm.jpa.JpaTransactionManager] - <Closing JPA EntityManager
[org.hibernate.ejb.EntityManagerImpl@4a005364] after transaction>
DEBUG [org.springframework.orm.jpa.EntityManagerFactoryUtils] - <Closing JPA EntityManager>
Contact count: 3
As shown in the previous output, you can see that the transaction for the countAll() was created
with read-only equaling true as expected.
But for the countAll() function, we don't want it to be enlisted in a transaction at all. The reason is
that we don't need the result to be managed by the underlying JPA EntityManager. Instead, we just want
to get the count and forget about it. In this case, we can override the transaction propagation behavior to
Propagation.NEVER. Listing 13-17 shows the revised countAll() method.
Listing 13-17. The ContactServiceImpl Class with Revised countAll() Implemented
package com.apress.prospring3.ch13.service.annotation;
// Import statement omitted
@Service("contactService")
@Repository
@Transactional
public class ContactServiceImpl implements ContactService {
// Codes omitted
@Transactional(propagation=Propagation.NEVER)
public long countAll() {
return contactRepository.count();
}
}
Run the testing code in Listing 13-16 again, and you will find that the transaction will not be created
for the countAll() method.
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home