我知道有很多关于交易的问题,但没有一个答案对我有帮助。所以,我正在尝试在我的Spring MVC应用程序中启用事务,但我不知道为什么它不起作用。这是我的配置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"/>
我尝试在我的服务类上使用@Transactional注释,在它的方法上,在DAO的方法上,但每次我得到相同的异常:
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
我认为我只是搞砸了配置,但以防其他类涉及处理。 这是控制器的方法:
@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
@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();
}
}
答案 0 :(得分:0)
我有两个配置文件:app-config.xml和web-config.xml。后者用于MVC配置。他们都有<context:component-scan base-package="ru.tsystems"/>
标签,看起来就是问题所在。这是论坛上的topic,帮助我解决了这个问题。
编辑:错字