spring配置了事务管理器,在dao层使用 @transactional 不起作用

来源:互联网  时间:2016/7/19 7:36:12

关于网友提出的“ spring配置了事务管理器,在dao层使用 @transactional 不起作用”问题疑问,本网通过在网上对“ spring配置了事务管理器,在dao层使用 @transactional 不起作用”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: spring配置了事务管理器,在dao层使用 @transactional 不起作用
描述:

spring管理事务session注释

spring配置了事务管理器,在dao层使用 @transactional 不起作用 ,求大神帮忙!!! 
spring配置如下:


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c//www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="byName" default-lazy-init="true">






 


<>
destroy-method="close">

<>
value="jdbc:mysql://localhost:3306/dagl2015?characterEncoding=utf8" />




<>
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">



com.dagl.manage.school.model
com.dagl.manage.admin.model
model
com.dagl.indexfront.model




org.hibernate.dialect.MySQLDialect
thread
true

true
true

org.hibernate.cache.EhCacheProvider
true 1, false 0, yes 'Y', no 'N'





com.dagl.indexfront.model.Maininformation








<>
class="org.springframework.orm.hibernate3.HibernateTransactionManager">



<>
class="org.springframework.transaction.interceptor.TransactionInterceptor">




PROPAGATION_REQUIRED




<>
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">


*ServiceImpl




transactionInterceptor




dao层代码:
package com.dagl.manage.admin.dao;
import static org.hibernate.criterion.Example.create;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dagl.manage.admin.model.Announcement;
@Transactional
@Service("announcementDao")
public class AnnouncementDAO {
private static final Logger log = LoggerFactory
.getLogger(AnnouncementDAO.class);
// property constants
public static final String ANN_TITLE = "annTitle";
public static final String ANN_SOURCE = "annSource";
public static final String ANN_COUNT = "annCount";
public static final String ANN_CONTENT = "annContent";
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
protected void initDao() {
// do nothing
}
public void save(Announcement transientInstance) {
log.debug("saving Announcement instance");
try {
getCurrentSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(Announcement persistentInstance) {
log.debug("deleting Announcement instance");
try {
getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public Announcement findById(java.lang.Integer id) {
log.debug("getting Announcement instance with id: " + id);
try {
Announcement instance = (Announcement) getCurrentSession().get(
"com.dagl.manage.admin.model.Announcement", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(Announcement instance) {
log.debug("finding Announcement instance by example");
try {
List results = (List) getCurrentSession()
.createCriteria("com.dagl.manage.admin.model.Announcement")
.add(create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
//测试保存
public void saveAnnouncement(Announcement announcement)
{
Session session = sessionFactory.getCurrentSession();
session.save(announcement);
tx.commit(); 
}
}
dao层的最后一个方法没用事务报错,加上事务就可以执行了

上一篇多线程问题 四个线程 两个线程对i +1 另个-1 新手求解答
下一篇让子类实例直接等于父类实例可以不可以?
明星图片
相关文章
《 spring配置了事务管理器,在dao层使用 @transactional 不起作用》由码蚁之家搜集整理于网络,
联系邮箱:mxgf168#qq.com(#改为@)