我的工作中,截至目前,我们有一个单一的数据源的Spring的MVC应用程序。 对于我们的要求,大多数的交易或者是短暂的,我们确实有这很少需要很长的时间来处理应用程序。 为了避免系统被塞得满满,因为这些要求,我们正计划建立一个单独的数据源。
除此之外,这些方法将有一个较低的优先级,因为用户知道这些将需要较长的时间。 对于这一点,我只是打电话Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
。 这是足够的? 主要的一点是,这些方法不应该消耗的CPU,磁盘,I / O等更多的时间
像其他弹簧MVC应用程序,我们有3层,控制器,服务,DAO。 一些服务层的方法将在这些数据源共享。 例如: long_method_service_layer()-->dependent_method_service_layer()
也short_method_service_layer()-->dependent_method_service_layer();
当第二个数据源调用dependent_method()所以,现在,将它也有一个低优先级?
目前,我们的服务层配置是这样的:
@Service
@Transactional
public class DownloadTokenServiceImpl implements DownloadTokenService{
private final DownloadTokenDAO downloadTokenDAO;
@Autowired
public DownloadTokenServiceImpl(DownloadTokenDAO downloadTokenDAO){
this.downloadTokenDAO = downloadTokenDAO;
}
@Override
public void method_name(){}
}
从第二数据源的方法,也将是这个类里面。 我打算用单独标注这些方法@Transactional("2nd_source")
这个对吗?
最后,错误日志从我尝试截至目前:
Caused by:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2: hibernate4AnnotatedSessionFactory_extended,hibernate4AnnotatedSessionFactory
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1133)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1021)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:814)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
at org.s
根context.xml中:
第一DATA_SOURCE(主):
<beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<beans:property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource"/>
<beans:property name="maximumPoolSize" value="50" />
<beans:property name="maxLifetime" value="200000" />
<beans:property name="idleTimeout" value="25000" />
<beans:property name="leakDetectionThreshold" value="200000"/>
<beans:property name="connectionTimeout" value="200000"/>
<beans:property name="dataSourceProperties">
<beans:props>
<beans:prop key="url">jdbc:postgresql://localhost:5432/DB_NAME</beans:prop>
<beans:prop key="user">username</beans:prop>
<beans:prop key="password">password</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="packagesToScan" value="com.our_app.spring.model"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
<beans:prop key="hibernate.jdbc.batch_size">50</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="cache.use_second_level_cache">true</beans:prop>
<beans:prop key="cache.use_query_cache">true</beans:prop>
<beans:prop key="hibernate.order_updates">true</beans:prop>
<beans:prop key="show_sql">false</beans:prop>
<beans:prop key="connection.release_mode">after_statement</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
</beans:bean>
第二个数据源(对于长时间运行的事务,阿光值需要一次它的工作进行调整):
<beans:bean id="extended_transactions_data_source" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
<beans:property name="dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource"/>
<beans:property name="maximumPoolSize" value="50" />
<beans:property name="maxLifetime" value="200000" />
<beans:property name="idleTimeout" value="25000" />
<beans:property name="leakDetectionThreshold" value="200000"/>
<beans:property name="connectionTimeout" value="200000"/>
<beans:property name="dataSourceProperties">
<beans:props>
<beans:prop key="url">jdbc:postgresql://localhost:5432/Db_NAME</beans:prop>
<beans:prop key="user">USERNAME</beans:prop>
<beans:prop key="password">passwoRD</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory_extended"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="extended_transactions_data_source"/>
<beans:property name="packagesToScan" value="com.our_app.spring.model"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
<beans:prop key="hibernate.jdbc.batch_size">50</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="cache.use_second_level_cache">true</beans:prop>
<beans:prop key="cache.use_query_cache">true</beans:prop>
<beans:prop key="hibernate.order_updates">true</beans:prop>
<beans:prop key="show_sql">false</beans:prop>
<beans:prop key="connection.release_mode">after_statement</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
<tx:annotation-driven transaction-manager="transactionManager_extended"/>
<beans:bean id="transactionManager_extended" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory_extended"/>
</beans:bean>