项目中要操作多个数据库的表,因此要配置多个数据源,配置如下:
<context:property-placeholder location="classpath:config/jdbc.properties" />
<!-- DataSource数据源,使用c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="minPoolSize" value="20"></property>
<property name="maxPoolSize" value="40"></property>
<property name="initialPoolSize" value="20"></property>
<property name="maxIdleTime" value="10000"></property>
<property name="acquireIncrement" value="10"></property>
<property name="maxStatements" value="0"></property>
<property name="idleConnectionTestPeriod" value="60"></property>
</bean>
<!-- pojo 映射 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config-service.xml"/>
<!--define entity package -->
<property name="typeAliasesPackage" value="com.app.anilys.dc.domain.*" />
</bean>
<!-- 数据源2配置 start -->
<bean id="activityDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.activity.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="minPoolSize" value="20"></property>
<property name="maxPoolSize" value="40"></property>
<property name="initialPoolSize" value="20"></property>
<property name="maxIdleTime" value="10000"></property>
<property name="acquireIncrement" value="10"></property>
<property name="maxStatements" value="0"></property>
<property name="idleConnectionTestPeriod" value="60"></property>
</bean>
<bean id="activitySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="activityDataSource" />
<property name="configLocation" value="classpath:mybatis/mybatis-config-activity.xml"/>
<property name="typeAliasesPackage" value="com.app.anilys.dc.domain.*" />
</bean>
<!-- 数据源2配置 end -->
<!-- 自动扫描组件,这里要把controler下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。 -->
<context:component-scan base-package="com.app.anilys.dc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- @AspectJ的支持,从而可以实现通过注解方式将通知编织到非公共方法中。 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 扫描mapper -->
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.app.anilys.dc.dao.*" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean name="activityMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.app.anilys.dc.dao.*" />
<property name="sqlSessionFactory" ref="activitySqlSessionFactory" />
</bean>
<!-- dao 事务 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 数据源2事务管理器配置 start -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="activityDataSource" />
</bean>
<!-- 数据源2事务管理器配置 end -->
<!-- spring 声明事务管理 -->
<aop:config>
<aop:pointcut id="fooServiceMethods" expression="execution(* com.app.anilys.dc.serviceimpl.*.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods"/>
</aop:config>
<aop:config>
<aop:pointcut id="controllerMethods" expression="execution(* com.app.anilys.dc.controller.*.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="controllerMethods"/>
</aop:config>
<!-- 数据源2事务通知配置 start -->
<aop:config>
<aop:pointcut id="activityFooServiceMethods" expression="execution(* com.app.anilys.dc.serviceimpl.*.*.*(..))"/>
<aop:advisor advice-ref="activityTxAdvice" pointcut-ref="activityFooServiceMethods"/>
</aop:config>
<aop:config>
<aop:pointcut id="activityControllerMethods" expression="execution(* com.app.anilys.dc.controller.*.*.*(..))"/>
<aop:advisor advice-ref="activityTxAdvice" pointcut-ref="activityControllerMethods"/>
</aop:config>
<!-- 数据源2事务通知配置 end -->
<!-- 事务管理配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="start*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="*" rollback-for="V20Exception"/>
</tx:attributes>
</tx:advice>
<!-- 数据源2事务通知配置 start -->
<tx:advice id="activityTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="select*" read-only="true" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" propagation="REQUIRED" />
<tx:method name="get*" read-only="true" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="add*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="delete*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="start*" propagation="REQUIRED" isolation="REPEATABLE_READ" />
<tx:method name="*" rollback-for="V20Exception"/>
</tx:attributes>
</tx:advice>
启动tomcat,访问工程应用,报如下错误:
org.springframework.jdbc.BadSqlGrammarException:
但是ActivityUser在其中上述配置文件数据源为 activityDataSource的库中存在的,说明这个数据源没有起作用,运行的时候还是在数据源为 dataSource的库中去找表,所以报如上错误,烦请各位帮我看看为何数据源 activityDataSource没起作用,多个数据源该如何配置和实现呢?
<!--流程引擎属性配置-->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="idGenerator" ref="midGenerator"/>
<property name="transactionManager" ref="transactionManager"/>
<property name="databaseSchemaUpdate" value="true"/>
<!-- 允许Activit更新数据表-->
<property name="activityFontName" value="宋体"/>
<property name="labelFontName" value="宋体"/>
<property name="jobExecutorActivate" value="false"/>
<property name="history" value="full"/>
<property name="processDefinitionCacheLimit" value="10"/>
</bean>
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。