zh02054 2014-03-10 08:32 采纳率: 0%
浏览 1399

请教: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就可以启用事物管理 -->




  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-01 20:29
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    这个问题涉及到Spring Boot应用程序中的事务管理和数据源切换。为了正确地解决你的问题,你需要按照以下步骤操作:

    1. 创建数据库连接池

      • application.propertiesapplication.yml文件中设置spring.datasource.urlspring.datasource.usernamespring.datasource.password等值。
      spring:
        datasource:
          url: jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC&useSSL=false
          username: root
          password: your_password_here
      
    2. 添加数据源

      • application.propertiesapplication.yml文件中设置spring.datasource.access-urlspring.datasource.access-userspring.datasource.access-password等值。
      spring:
        datasource:
          access-url: jdbc:mysql://localhost:3307/testdb?serverTimezone=UTC&useSSL=false
          access-user: root
          access-password: your_access_password_here
      
    3. 配置事务管理器

      • 创建一个新的类来实现@Transactional注解,并将它与@Component注解一起使用。
      @Component
      public class AccessTransactionManager implements TransactionManager {
        // 实现所需的方法
      }
      
    4. 配置事务管理器

      • application.propertiesapplication.yml文件中添加spring.transaction.automatic配置项为false,这将导致事务管理器无法自动管理事务。
      spring:
        transaction:
          automatic: false
      
    5. 测试

      • 使用@Transactional注解来测试你的业务逻辑。

    这样,你就可以在一个新的线程上运行事务了。如果需要在其他线程上执行事务,请确保每个线程都具有正确的权限。

    评论

报告相同问题?