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>
在我使用sessionFactory.getCurrentSession();时就会抛出 No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here错误
求解释这是怎么一回事 好像事务没代理进去!网上好像看到有解决办法用注解 但是我这都是配置的也不习惯用注解的方法
openSession是可以的 但是也有人测试过性能方面的问题链接如下
http://www.iteye.com/topic/1113462
贴上类代码
业务逻辑类
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>
网上找的很多都是一样的 也没什么参考性
有的说加上这个 <prop key="hibernate.current_session_context_class">thread</prop>
但是还是得手动配置事务
求各位大神给个解决办法!
...全文
相关推荐
ssh_inte2模板常用方法&延迟加载问题
原因是延迟加载还没有发生SQL语句,在业务层session对象就已经销毁了,所以查询到的JavaBean对象已经变成了托管态对象
SSH 银行声明式事务
NULL 博文链接:https://chaoyi.iteye.com/blog/2168861
SSH事务配置大全
各种事物配置方法。用于工作学习都可以获得很好的知识积累
SSH整合问题
在整合SSH时出现了一个bug,很长时间了都没用解决,忘大侠们帮助,发到邮箱kxf327@qq.com
SSH搭建时的关键内容,困扰你我的关键点
几种SSH的搭配 固定一种形式 配置完后重复包的检查 事物配置的详解


