Spring Boot错误,涉及多个数据源(无法将DialectResolutionInfo访问为空)

huangapple go评论101阅读模式
英文:

Spring Boot Error With Multiple datasources (Access to DialectResolutionInfo cannot be null)

问题

以下是翻译好的内容:

我是一个初学者,参考了这个教程创建了一个简单的网络服务应用:https://spring.io/guides/gs/accessing-data-mysql/

然后,我尝试对访问第二个数据库进行了一些修改,但是遇到了错误。
我检查了我的数据库及其用户,它们看起来都没问题。

有人能帮我解决这个问题吗?谢谢!

  1. java.lang.IllegalStateException: 无法加载应用程序上下文
  2. 由于org.springframework.beans.factory.BeanCreationException: TableReady.WebService.Repositories.UserRepository 中定义的名为 'userRepository' bean 在声明在 JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration 上的 @EnableJpaRepositories 中定义在设置 bean 属性 'mappingContext' 时无法解析对 bean 'jpaMappingContext' 的引用嵌套异常是 org.springframework.beans.factory.BeanCreationException: 创建名为 'jpaMappingContext' bean 时出错初始化方法的调用失败嵌套异常是 org.hibernate.service.spi.ServiceException: 无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  3. 由于org.springframework.beans.factory.BeanCreationException: 创建名为 'jpaMappingContext' bean 时出错初始化方法的调用失败嵌套异常是 org.hibernate.service.spi.ServiceException: 无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  4. 由于org.hibernate.service.spi.ServiceException: 无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  5. 由于org.hibernate.HibernateException: 当未设置 'hibernate.dialect' 无法将访问 DialectResolutionInfo 设置为空

application.properties

  1. spring.jpa.hibernate.ddl-auto=update
  2. spring.datasource1.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/users
  3. spring.datasource1.username=user_info_admin
  4. spring.datasource1.password=(@.SjFHn8LqdL)7/
  5. spring.datasource2.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/general
  6. spring.datasource2.username=general_admin
  7. spring.datasource2.password=sP7Y86.=-k<hKSgG

JpaConfig.java

  1. @Configuration
  2. public class JpaConfig {
  3. @Bean
  4. @Primary
  5. @ConfigurationProperties(prefix="spring.datasource1")
  6. public DataSource primaryDataSource() {
  7. return DataSourceBuilder.create().build();
  8. }
  9. @Bean
  10. @ConfigurationProperties(prefix="spring.datasource2")
  11. public DataSource secondaryDataSource() {
  12. return DataSourceBuilder.create().build();
  13. }
  14. }

MainController.java

  1. @Controller // 表示这个类是一个控制器
  2. @RequestMapping(path="/users") // 表示 URL 以 /demo 开头(在应用路径之后)
  3. public class MainController {
  4. // 这表示要获取名为 userRepository 的 bean
  5. // 由 Spring 自动生成,我们将使用它来处理数据
  6. @Autowired
  7. private UserRepository userRepository;
  8. @Autowired
  9. private RestaurantRepository restaurantRepository;
  10. // 其他内容...
  11. // ...
  12. // ...
  13. }

Restaurant.java

  1. @Entity
  2. @Table(name="restaurants", schema="general")
  3. public class Restaurant {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private Integer id;
  7. private String name;
  8. private String address;
  9. private String email;
  10. private String phone;
  11. // ...
  12. // ...
  13. }

User.java

  1. @Entity
  2. @Table(name="users", schema="users")
  3. public class User {
  4. @Id
  5. @GeneratedValue(strategy=GenerationType.AUTO)
  6. private Integer id;
  7. private String password;
  8. private String name;
  9. private String email;
  10. private String phone;
  11. // ...
  12. // ...
  13. }

两个仓库看起来像这样:

  1. public interface SomeRepository extends CrudRepository<User, Integer> {
  2. }

图片描述

英文:

I am a starter, and I created a simple web service app by refering this tutorial: https://spring.io/guides/gs/accessing-data-mysql/

And then I tried to do some modifications for accesing the second database and I got an error for that.
I checked my database and its users, and they seem okay.

Can anyone help me with it? Thanks!

  1. java.lang.IllegalStateException: Failed to load ApplicationContext
  2. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;userRepository&#39; defined in TableReady.WebService.Repositories.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean &#39;jpaMappingContext&#39; while setting bean property &#39;mappingContext&#39;; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;jpaMappingContext&#39;: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  3. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;jpaMappingContext&#39;: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  4. Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
  5. Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when &#39;hibernate.dialect&#39; not set

enter image description here

application.properties

  1. spring.jpa.hibernate.ddl-auto=update
  2. spring.datasource1.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/users
  3. spring.datasource1.username=user_info_admin
  4. spring.datasource1.password=(@.SjFHn8LqdL)7/
  5. spring.datasource2.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/general
  6. spring.datasource2.username=general_admin
  7. spring.datasource2.password=sP7Y86.=-k&lt;hKSgG

JpaConfig.java

  1. @Configuration
  2. public class JpaConfig {
  3. @Bean
  4. @Primary
  5. @ConfigurationProperties(prefix=&quot;spring.datasource1&quot;)
  6. public DataSource primaryDataSource() {
  7. return DataSourceBuilder.create().build();
  8. }
  9. @Bean
  10. @ConfigurationProperties(prefix=&quot;spring.datasource2&quot;)
  11. public DataSource secondaryDataSource() {
  12. return DataSourceBuilder.create().build();
  13. }
  14. }

MainController.java

  1. @Controller // This means that this class is a Controller
  2. @RequestMapping(path = &quot;/users&quot;) // This means URL&#39;s start with /demo (after Application path)
  3. public class MainController {
  4. // This means to get the bean called userRepository
  5. // Which is auto-generated by Spring, we will use it to handle the data
  6. @Autowired
  7. private UserRepository userRepository;
  8. @Autowired
  9. private RestaurantRepository restaurantRepository;
  10. // something else...
  11. // ...
  12. // ...
  13. }

Restaurant.java

  1. @Entity
  2. @Table(name = &quot;restaurants&quot;, schema = &quot;general&quot;)
  3. public class Restaurant {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.AUTO)
  6. private Integer id;
  7. private String name;
  8. private String address;
  9. private String email;
  10. private String phone;
  11. // ...
  12. // ...
  13. }

User.java

  1. @Entity
  2. @Table(name = &quot;users&quot;, schema = &quot;users&quot;)
  3. public class User {
  4. @Id
  5. @GeneratedValue(strategy=GenerationType.AUTO)
  6. private Integer id;
  7. private String password;
  8. private String name;
  9. private String email;
  10. private String phone;
  11. // ...
  12. // ...
  13. }

Both repos look like:

  1. public interface SomeRepository extends CrudRepository&lt;User, Integer&gt; {
  2. }

答案1

得分: 0

这个问题可能是由于数据库服务器宕机或无法访问造成的。请检查数据库服务器是否正在运行。

更详细的答案可以在这里找到。

英文:

This issue could be caused by the database server being down or not accessible. Please check if the database server is running.

More detailed answers are available here

huangapple
  • 本文由 发表于 2020年10月11日 06:50:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/64299029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定