You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/06/07 03:53:11 UTC

[GitHub] [shardingsphere] phdbutbachelor commented on issue #18178: Shardingsphere-jdbc single db , how to config local transaction with spring

phdbutbachelor commented on issue #18178:
URL: https://github.com/apache/shardingsphere/issues/18178#issuecomment-1148164639

   you can inject a ShardingSphereDataSource bean into a LocalSesstionFactoryBean, then a PlatformTransactionManager:
   -----------------------------------------------------------------------------------------------------------------------------------
   @Bean
   public DataSource mainShardingSphereDataSource() throws IOException, SQLException {
       File _yaml = ResourceUtils.getFile("classpath:shardingsphere.yml");
       return YamlShardingSphereDataSourceFactory.createDataSource(_yaml);
   }
   
   @Bean
   public LocalSessionFactoryBean mainShardingSphereSessionFactory(@Value(value = "${spring.datasource.main.dialect}") String dialect, @Qualifier(value = "mainShardingSphereDataSource") DataSource dataSource) {
       LocalSessionFactoryBean _localSessionFactoryBean = new LocalSessionFactoryBean();
       _localSessionFactoryBean.setDataSource(dataSource);
       _localSessionFactoryBean.setPackagesToScan("gmcc.sparrow.model.set");
   
       Properties _properties = new Properties();
       _properties.setProperty("hibernate.dialect", dialect); // 设置方言
       _localSessionFactoryBean.setHibernateProperties(_properties);
       return _localSessionFactoryBean;
   }
   
   @Bean
   public PlatformTransactionManager mainShardingSphereTransactionManager(@Qualifier(value = "mainShardingSphereSessionFactory") EntityManagerFactory entityManagerFactory) {
       return new JpaTransactionManager(entityManagerFactory);
   }
   -----------------------------------------------------------------------------------------------------------------------------------


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org