SSH2 事务代理问题
<!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><!-- <bean id="sessionFactory" class="org.springframework.orm.toplink.LocalSessionFactoryBean"> --> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>com/onionportal/orm/SystemLog.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <!--这里设置getcurrentSession的作用域--><!-- <prop key="hibernate.current_session_context_class">thread</prop>--> <!--设置连接对象--> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!--输出sql--> <prop key="hibernate.show_sql">true</prop> <!--格式化SQL--> <prop key="hibernate.format_sql">true</prop> <!-- 开启二级缓存--> <prop key="hibernate.cache.use_second_level_cache">true</prop> <!-- 是否缓存查询结果 --> <prop key="hibernate.cache.use_query_cache">true</prop> <!-- 设置缓存类--> <prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop> <!-- 设置memcache缓存服务器端口 --> <prop key="hibernate.memcached.servers">192.168.1.110:11211</prop> <!-- 设置二级缓存的前缀名称 --> <prop key="hibernate.cache.region_prefix">quality.cache.onionPortalCache</prop> <!-- 否使用结构化的方式缓存对象 --> <prop key="hibernate.cache.use_structured_entries">true</prop> </props> </property> </bean> <!--定义Hibernate的事务管理器HibernateTransactionManager --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 依赖注入上面定义的sessionFactory --> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!--定义Spring的事务拦截器TransactionInterceptor --> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 依赖注入上面定义的事务管理器transactionManager --> <property name="transactionManager" ref="transactionManager"/> <!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 --> <property name="transactionAttributes"> <props> <!-- 以browse、list、load、get及is开头的所有方法采用只读型事务控制类型 --> <prop key="browse*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop> <!-- 所有方法均进行事务控制,如果当前没有事务,则新建一个事务 --> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 定义BeanNameAutoProxyCreatorf进行Spring的事务处理--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 针对指定的bean自动生成业务代理 --> <property name="beanNames"> <list> <value>testtransactionService</value> <value>systemHtmlService</value> </list> </property><!-- 这个属性为true时,表示被代理的是目标类本身而不是目标类的接口 --><!-- <property name="proxyTargetClass">--><!-- <value>true</value>--><!-- </property>--><!-- 依赖注入上面定义的事务拦截器transactionInterceptor --><!-- <property name="interceptorNames">--><!-- <list>--><!-- <value>transactionInterceptor</value> --><!-- </list>--><!-- </property>--> </bean>
public class TesttransactionServiceimp implements TesttransactionService{ private TesttransactionDao testtransactionDao; public void Testtransaction() { try { testtransactionDao.save1(); testtransactionDao.save2(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public class TesttransactionDaoimp implements TesttransactionDao{ private SessionFactory sessionFactory; public Session getSession(){ return sessionFactory.getCurrentSession(); } public void save1() throws Exception{ Session se=this.getSession(); SystemLog log=new SystemLog(); log.setCreateTime("2012-07-31 00:00:00"); log.setMessageBody("dsf"); log.setMessageLevel(1); log.setStackTrace("111"); log.setUserId(1); se.save(log); } public void save2() throws Exception{ Session se=this.getSession(); SystemLog log=new SystemLog(); se.save(log); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }
<bean id="testtransactionDao" class="com.onionportal.test.dao.imp.TesttransactionDaoimp"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="testtransactionService" class="com.onionportal.test.service.imp.TesttransactionServiceimp"> <property name="testtransactionDao" ref="testtransactionDao"></property> </bean> <bean id="testTransaction" class="com.onionportal.until.task.TestTransaction"> <property name="testtransactionService" ref="testtransactionService"></property> </bean>
import java.io.Serializable;import java.util.List;import org.hibernate.Query;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;/** 继承Spring的HibernateDaoSupport*/public abstract class HibernateBaseDao extends HibernateDaoSupport { @SuppressWarnings("unchecked") public abstract Class getEntityClass(); public abstract String getEntityType(); //添加对象操作 public void save(Serializable entity) { getHibernateTemplate().save(entity); } //修改对象操作 public void update(Serializable entity) { getHibernateTemplate().update(entity); } //删除对象操作 public void delete(int objId){ getHibernateTemplate().delete(getHibernateTemplate().load(getEntityClass(), objId)); } //查询对象列表 @SuppressWarnings("unchecked") public List getAll(int count, int start) { String hql = "from " + getEntityType() + " m "; Query q = getHibernateTemplate().getSessionFactory() .openSession().createQuery(hql); q.setMaxResults(count); q.setFirstResult(start); return q.list(); }}