2 zh02054 zh02054 于 2014.03.10 16:32 提问

请教:spring配置mysql/access多数据源的动态切换问题,急用,十分感谢!

请教:spring配置mysql/access多数据源在切换到access数据源时抛No Session found for current thread错误是怎么回事?请大侠详加指导,十分感谢,急用!我的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd" default-autowire="byName" default-lazy-init="false">
<!-- 自动扫描dao和service包(自动注入) -->

<!-- 加载service,此时要排除要controller,因为controller已经spring-mvc中加载过了 -->


/context:component-scan

<!-- 引入属性文件 -->

<!-- 配置数据源1 -->




<!-- 初始化连接大小 -->

<!-- 连接池最大使用连接数量 -->

<!-- 连接池最大空闲 -->

<!-- 连接池最小空闲 -->

<!-- 获取连接最大等待时间 -->

<!-- -->



    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000" />
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="25200000" />

    <!-- 打开removeAbandoned功能 -->
    <property name="removeAbandoned" value="true" />
    <!-- 1800秒,也就是30分钟 -->
    <property name="removeAbandonedTimeout" value="1800" />
    <!-- 关闭abanded连接时输出错误日志 -->
    <property name="logAbandoned" value="true" />

<!-- 数据源2 :  access数据源 -->
<bean id="accessDataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver" />
    <property name="url" value="jdbc:odbc:users" />
    <property name="username" value="" />
    <property name="password" value="" />
</bean>
 <!-- access sessionFactory -->
<bean id="aceessSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="accessDataSource" />
    <property name="hibernateProperties">
        <props>
            <!-- access 语法和MSSQL相似 所以用的MSSQL方言,或者可以使用第三方方言 -->
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.jdbc.batch_size">30</prop>
            <prop key="hibernate.jdbc.fetch_size">50</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">false</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>

<!-- 数据源集合 -->













${hibernate.dialect}
${hibernate.hbm2ddl.auto}
true
true
false


<!-- 注解方式配置 -->


system.pojo.*
demo.entity.*
test.entity.*
test.bussiness.db.UserInfo






<bean id="accessTransactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="aceessSessionFactory"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="accessTransactionManager">
    <tx:attributes>
        <tx:method name="exists" read-only="true" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="create*" propagation="REQUIRED" />
        <tx:method name="insert*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="merge*" propagation="REQUIRED" />
        <tx:method name="del*" propagation="REQUIRED" />
        <tx:method name="remove*" propagation="REQUIRED" />
        <tx:method name="put*" propagation="REQUIRED" />
        <tx:method name="use*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="REQUIRED" />
        <tx:method name="count*" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />
        <tx:method name="list*" propagation="REQUIRED" read-only="true" />
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>



/aop:config







<!-- 配置事物管理器,在*ServiceImpl里写@Transactional就可以启用事物管理 -->




Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!