已经建立连接之后未强制实施连接只读方式。 要强制实施只读连接,应设置只读数据源或连接属性。 ERRORCODE=4474, SQLSTATE=01000

http://forum.springframework.org/showthread.php?t=12637


Well, the read-only flag is a kind of stepchild in the JDBC spec... Many drivers (and DBMS) don't really support a true read-only transaction. Some do optimize transaction processing, though, so setting the readOnly flag is nevertheless usually worth it.

In your case, the driver simply isn't able to switch the connection to true read-only mode and logs a corresponding warning. This can safely be ignored; your transaction won't get read-only optimizations, but that doesn't hurt.

You could remove the "readOnly" marker from your transaction attribute to avoid the warning. However, "readOnly" also triggers other optimizations, for example it suppresses Hibernate flush attempts for read-only operations, so I recommend to keep those markers and simply ignore the warnings.


------------------------------------------------

http://blog.csdn.net/MageShuai/article/details/4544191


概念:从这一点设置的时间点开始(时间点a)到这个事务结束的过程中,其他事务所提交的数据,该事务将看不见!(查询中不会出现别人在时间点a之后提交的数据)

应用场合:

如果你一次执行单条查询语句,则没有必要启用事务支持,数据库默认支持SQL执行期间的读一致性; 
如果你一次执行多条查询语句,例如统计查询,报表查询,在这种场景下,多条查询SQL必须保证整体的读一致性,否则,在前条SQL查询之后,后条SQL查询之前,数据被其他用户改变,则该次整体的统计查询将会出现读数据不一致的状态,此时,应该启用事务支持。
【注意是一次执行多次查询来统计某些信息,这时为了保证数据整体的一致性,要用只读事务】

 

怎样设置:

对于只读查询,可以指定事务类型为readonly,即只读事务。
由于只读事务不存在数据的修改,因此数据库将会为只读事务提供一些优化手段,例如Oracle对于只读事务,不启动回滚段,不记录回滚log。

(1)在JDBC中,指定只读事务的办法为: connection.setReadOnly(true);

(2)在Hibernate中,指定只读事务的办法为: session.setFlushMode(FlushMode.NEVER); 
此时,Hibernate也会为只读事务提供Session方面的一些优化手段

(3)在Spring的Hibernate封装中,指定只读事务的办法为: bean配置文件中,prop属性增加“readOnly”
或者用注解方式@Transactional(readOnly=true)
【 if the transaction is marked as read-only, Spring will set the Hibernate Session’s flush mode to FLUSH_NEVER, 
and will set the JDBC transaction to read-only】也就是说在Spring中设置只读事务是利用上面两种方式

 

在将事务设置成只读后,相当于将数据库设置成只读数据库,此时若要进行写的操作,会出现错误

---------------------------------------------------------

spring配置文件配置方法,read-only


<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">     
  <property name="sessionFactory">     
   <ref local="sessionFactory" />     
  </property>     
</bean>      
    <!-- Transactional advice -->   
<tx:advice id="txAdvice" transaction-manager="transactionManager">   
    <tx:attributes>   
        <!-- methods starting with 'save', 'update' or 'remove' use the default transaction settings -->
        <tx:method name="save*"/>   
        <tx:method name="update*"/>   
        <tx:method name="remove*"/>   
        <!-- other methods are set to read only -->   
        <tx:method name="*" read-only="true"/>   
    </tx:attributes>   
</tx:advice>   
  
    <!-- ensure that the above transactional advice runs for any execution of an operation defined by specified interface -->   
<aop:config>   
    <aop:pointcut id="daoOperation"  
        expression="execution(* com.DAO.BasicDAO.*(..))"/>   
    <aop:advisor advice-ref="txAdvice" pointcut-ref="daoOperation"/>   
</aop:config>   
  
<bean id="BasicDAO" class="com.DAO.BasicDAO">     
    <property name="sessionFactory">     
        <ref local="sessionFactory" />     
    </property>     
</bean>   
  
<bean name="/login" class="com.struts.LoginAction" singleton="false">     
    <property name="basicDAO">     
        <ref bean="BasicDAO" />     
    </property>     

</bean> 


-------------2014-09-04

如果可能,尽量把多次查询写成一个sql语句。用来保持事务的一致性。



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值