Hibernate @Transactional not starting transaction
Posted
by
rhinds
on Stack Overflow
See other posts from Stack Overflow
or by rhinds
Published on 2012-03-26T21:26:52Z
Indexed on
2012/03/29
11:29 UTC
Read the original article
Hit count: 231
hibernate
I have a web app using Hibernate, and I am attempting to persist some data, but it is failing to persist within a Transaction despite using the @Transactional annotation.
My service class is as follows:
@Service("profileService")
public class ProfileService {
private EntityManager entityManager;
@Autowired
private AccountService accountService;
@Autowired
private ProfileDAOImpl profileDao;
@PersistenceContext
public void setEntityManager(EntityManager em) {
this.entityManager = em;
}
@Transactional
public void addConnectionToAccount(SocialConnection sc) {
entityManager.persist(sc);
}
}
The addConnectionToAccount() method is being called from another Spring bean in a normal method, and the ProfileService class is currently being injected there:
public class HibernateConnectionRepository implements ConnectionRepository {
@Inject
private ProfileService profileService;
@Override
@Transactional
public void addConnection(SocialConnection sc) {
try {
profileService.addConnectionToAccount(accountId, sc);
} catch (Exception e) {
e.printStackTrace();
}
}
I tried putting the @Transactional annotation on the calling method in the vain hope that it might make a difference but nothing.
Previously I have experienced problems like this its been because the object being persisted does not satisfy table restrictions (such as non-nullable columns as null) or because the method is being called from within the same class and the calling method is not Transactional, but neither of those are the case here..
Any ideas? it just fails silently, the logs are as follows:
2012-03-26 22:25:04,702 [http-bio-8085-exec-9] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@1bc25c8 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@e5b006)
2012-03-26 22:25:04,710 [http-bio-8085-exec-9] DEBUG org.hibernate.SQL - select SEQ_COUNT from SEQUENCE where SEQ_NAME = 'PO_SEQ' for update
2012-03-26 22:25:04,711 [http-bio-8085-exec-9] DEBUG org.hibernate.SQL - update SEQUENCE set SEQ_COUNT = ? where SEQ_COUNT = ? and SEQ_NAME = 'PO_SEQ'
2012-03-26 22:25:04,723 [http-bio-8085-exec-9] DEBUG com.mchange.v2.resourcepool.BasicResourcePool - trace com.mchange.v2.resourcepool.BasicResourcePool@1bc25c8 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@e5b006)
2012-03-26 22:25:04,723 [http-bio-8085-exec-9] DEBUG org.hibernate.event.internal.AbstractSaveEventListener - Generated identifier: 2200, using strategy: org.hibernate.id.MultipleHiLoPerTableGenerator
UPDATE
Also wanted to mention that the HibernateConnectionRepository bean is not annotated and is actually being configured in an @Configuration class (if this makes any difference? not used @Configuration classes much).
The method to create the bean is as follows:
@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
public ConnectionRepository connectionRepository() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in");
}
ApplicationUser user = (ApplicationUser) authentication.getPrincipal();
return usersConnectionRepository().createConnectionRepository(String.valueOf(user.getAccountId()));
}
The bean is scoped to the logged in user, but may also be created multiple times for each user..
© Stack Overflow or respective owner