英文:
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 'userRepository' defined in TableReady.WebService.Repositories.UserRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': 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 'jpaMappingContext': 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 'hibernate.dialect' not set
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 // This means that this class is a Controller
@RequestMapping(path = "/users") // This means URL'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 = "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;
// ...
// ...
}
Both repos look like:
public interface SomeRepository extends CrudRepository<User, Integer> {
}
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论