I know there's a lot of questions about transactions but none of the answers helped me. So, I'm trying to enable transactions in my Spring MVC application but I've no idea why it doesn't work. Here's my config xml
<context:component-scan base-package="ru.tsystems"/>
<bean id="ecareEMF" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="ru.tsystems.persistence.entity"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<!--Some problems with server's time/date-->
<property name="url"
value="jdbc:mysql://localhost:3306/ecare?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="ecareEMF"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
I've tried to use @Transactional annotation on my service class, on it's method, on method's of DAO but every time I get the same exception:
2018-04-06 01:27:04 DEBUG UserDAOImpl:34 - javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'persist' call
I think I just messed up with configurations, but just in case here's other classes involved in processing. Here's method of controller:
@RequestMapping(value = "/greeting", method = RequestMethod.POST)
public @ResponseBody
String greeting(@Valid UserDTO userDTO, BindingResult result) {
logger.debug("Attempting to save " + userDTO.toString() + " in db.");
userMngService.save(userDTO.convertToUser());
return userDTO.toString();
}
Service:
@Service
@Transactional
public class UserMngService {
@Autowired
private UserDAO userDAO;
public void save(User user) {
userDAO.persist(user);
}
}
DAO:
@Repository
public class UserDAOImpl extends GenericDAOImpl<User, Long> implements UserDAO {
}
Generic DAO:
public abstract class GenericDAOImpl<E, K> implements GenericDAO<E, K> {
protected Class<E> entityClass;
@PersistenceContext
private EntityManager entityManager;
private static final Logger logger = Logger.getLogger(UserDAOImpl.class);
@SuppressWarnings("unchecked")
public GenericDAOImpl(){
entityClass = (Class<E>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
@Override
public void persist(E entity) {
try{
entityManager.persist(entity);
}catch (PersistenceException e){
logger.debug(e.toString());
}
}
@Override
public void delete(E entity) {
try{
entityManager.remove(entity);
}catch (PersistenceException e){
}
}
@Override
public void update(E entity) {
try{
entityManager.merge(entity);
}catch (PersistenceException e){
}
}
@Override
public E get(K id) {
try{
return entityManager.find(entityClass, id);
}catch (PersistenceException e){
return null;
}
}
@Override
public List<E> getAll() {
throw new UnsupportedOperationException();
}
}
I had two config files: app-config.xml and web-config.xml. The latter was for MVC configuration. They both had <context:component-scan base-package="ru.tsystems"/>
tag and looks like that was the problem. Here's topic on the forum that helped me to resolve it.
EDIT: typo
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments