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

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

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

问题

以下是翻译好的内容:

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

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

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

java.lang.IllegalStateException: 无法加载应用程序上下文
由于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]
由于org.springframework.beans.factory.BeanCreationException: 创建名为 'jpaMappingContext' 的 bean 时出错初始化方法的调用失败嵌套异常是 org.hibernate.service.spi.ServiceException: 无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
由于org.hibernate.service.spi.ServiceException: 无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
由于org.hibernate.HibernateException: 当未设置 'hibernate.dialect'无法将访问 DialectResolutionInfo 设置为空

application.properties

spring.jpa.hibernate.ddl-auto=update

spring.datasource1.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/users
spring.datasource1.username=user_info_admin
spring.datasource1.password=(@.SjFHn8LqdL)7/

spring.datasource2.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/general
spring.datasource2.username=general_admin
spring.datasource2.password=sP7Y86.=-k<hKSgG

JpaConfig.java

@Configuration
public class JpaConfig {
    @Bean
    @Primary
    @ConfigurationProperties(prefix="spring.datasource1")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix="spring.datasource2")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

MainController.java

@Controller // 表示这个类是一个控制器
@RequestMapping(path="/users") // 表示 URL 以 /demo 开头(在应用路径之后)
public class MainController {

    // 这表示要获取名为 userRepository 的 bean
    // 由 Spring 自动生成,我们将使用它来处理数据
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RestaurantRepository restaurantRepository;

    // 其他内容...
    // ...
    // ...
}

Restaurant.java

@Entity
@Table(name="restaurants", schema="general")
public class Restaurant {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;
    private String address;
    private String email;
    private String phone;
    // ...
    // ...
}

User.java

@Entity
@Table(name="users", schema="users")
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String password;
    private String name;
    private String email;
    private String phone;
    // ...
    // ...
}

两个仓库看起来像这样:

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

图片描述

英文:

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!

java.lang.IllegalStateException: Failed to load ApplicationContext
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]
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]
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when &#39;hibernate.dialect&#39; not set

enter image description here

application.properties

spring.jpa.hibernate.ddl-auto=update

spring.datasource1.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/users
spring.datasource1.username=user_info_admin
spring.datasource1.password=(@.SjFHn8LqdL)7/

spring.datasource2.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/general
spring.datasource2.username=general_admin
spring.datasource2.password=sP7Y86.=-k&lt;hKSgG

JpaConfig.java

@Configuration
public class JpaConfig {
    @Bean
    @Primary
    @ConfigurationProperties(prefix=&quot;spring.datasource1&quot;)
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix=&quot;spring.datasource2&quot;)
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

MainController.java

@Controller // This means that this class is a Controller
@RequestMapping(path = &quot;/users&quot;) // This means URL&#39;s start with /demo (after Application path)
public class MainController {

    // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RestaurantRepository restaurantRepository;

    // something else...
    // ...
    // ...
}

Restaurant.java

@Entity
@Table(name = &quot;restaurants&quot;, schema = &quot;general&quot;)
public class Restaurant {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String name;
    private String address;
    private String email;
    private String phone;
    // ...
    // ...
}

User.java

@Entity
@Table(name = &quot;users&quot;, schema = &quot;users&quot;)
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    private String password;
    private String name;
    private String email;
    private String phone;
    // ...
    // ...
}

Both repos look like:

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

答案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:

确定