shiro会话更新:连接是只读的。不允许导致数据修改的查询

时间:2018-01-23 07:17:51

标签: java spring session transactions shiro

我们使用java SSH,最近我们经常遇到以下问题,不确定发生了什么,我搜索了很多次并没有发生过这样的shiro相关场景,我们使用shiro作为authenticerticaiton框架,并定制了sessionDAO,包括会话操作,如& #34; doCreate,doUpdate等。",甚至在applicaitonContext.xml中配置如下:

 <tx:method name="do*" propagation="REQUIRES_NEW" /> 

追踪:

2018-01-22 18:02:03.482 [http-nio-8080-exec-762] INFO  org.apache.struts2.rest.RestActionInvocation - Executed action [//order/order!index!xhtml!200] took 574 ms (execution: 149 ms, result: 425 ms)
org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [update sessions set session=? where session_id=?]; Connection is read-only. Queries leading to data modification are not allowed; nested exception is java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
        at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:108)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
        at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:649)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:870)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:931)
        at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:941)
        at com.shopping.web.authentication.dao.impl.ShiroSessionDao.doUpdate(ShiroSessionDao.java:48)
        at org.apache.shiro.session.mgt.eis.CachingSessionDAO.update(CachingSessionDAO.java:277)
        at org.apache.shiro.session.mgt.eis.CachingSessionDAO$$FastClassBySpringCGLIB$$2a5e5afd.invoke(<generated>)
有人可以帮忙吗? 非常感谢。

applicationContext.xml中的db配置:

 <!--  C3P0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>

        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
        <property name="maxIdleTime" value="${cpool.maxIdleTime}"/> 
    </bean>        
  <!-- Spring jdbcTempate used for authentication -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
       <property name="dataSource" ref="dataSource"></property>  
   </bean>

    <!--SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

        <property name="mappingLocations" value="classpath:com/shopping/web/entities/*.hbm.xml"></property>
        <property name="packagesToScan">  
            <list>  
                <value>com.shopping</value>  
            </list>  
        </property>
    </bean>

we use jdbctemplate together with hibernate5 with the same session managment.

    in db.proerties:
    jdbc.user=shopping
    jdbc.password=123456
    jdbc.driverClass=com.mysql.jdbc.Driver
  jdbc.jdbcUrl=jdbc:mysql://192.168.2.221:3306,192.168.2.222:3306,192.168.2.200:3306/shopping?useUnicode=true&characterEncoding=utf-8

    jdbc.initPoolSize=5
    jdbc.maxPoolSize=10
    cpool.maxIdleTime=25200

1 个答案:

答案 0 :(得分:0)

在努力解决这个问题之后,我们找到了根本原因: 我们在服务器端使用mysql cluster和c3p0连接池来存储数据以及shiro会话,如果池中没有可用的连接,有时网站需要重新连接到mysql集群,一旦重新连接,就会读取默认模式只有,但是shiro会话需要在数据库中进行更新操作,这会导致问题。 解决方案是:更改mysql连接URL如下: ?jdbc.jdbcUrl = JDBC:MySQL的:// server1的:3306,服务器2:3306,服务器3:3306 /数据库autoReconnectForPools =真安培; autoReconnect的=真安培; failOverReadOnly =假安培;了useUnicode =真安培;的characterEncoding = UTF-8 和autoReconnectForPools = true&amp; autoReconnect = true&amp; failOverReadOnly = false是解决方案的关键