我的应用程序有两个数据源。第一个数据源使用JdbcTemplate访问Postgre。第二个数据源使用Spring数据JPA访问CockroachDb。
所以我为这个数据源创建了两种配置。
除了具有空间数据的对象之外,坚持使用这两个数据源工作正常。当我试图保存空间数据到CockroachDb,我得到一个错误:
Hibernate:选择geoobject0_. id为id1_2_0_,geoobject0_.地址为address2_2_0_,geoobject0_.address_id为address_3_2_0_,geoobject0_.创建为created4_2_0_,geoobject0_.flat_count为flat_cou5_2_0_,geoobject0_.geo_provider_id为geo_prov6_2_0_,geoobject0_.几何为geometry7_2_0_,geoobject0_.hq_id为hq_id8_2_0_,geoobject0_is_generated为is_gener9_2_0_,geoobject0_.is_living为is_livi10_2_0_,geoobject0_payload11_2_0_,geoobject0_。source_idsource_12_2_0_,geoobject0_。键入type13_2_0_,geoobject0_。uic_iduic_id14_2_0_,geoobject0_。version_idversion15_2_0_,geoobject0_。voter_countvoter_c16_2_0_geo_service.geo_objectgeoobject0_geoobject0_。Hibernate:插入到geo_service.geo_object(地址,address_id,创建,flat_count,geo_provider_id,几何,hq_id,is_generated,is_living,有效载荷,source_id,类型,uic_id,version_id,voter_count,id)值 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?)2022-01-18 17:01:39.583[ForkJoinPool.common池工人-3]WARNo.h.e.jdbc.spi.SqlExceptionHelper logExceptions()-SQL错误: 0, SQLState: XXUUU2022-01-18 17:01:39.584[ForkJoinPool.common池工人-3]ERRORo.h.e.jdbc.spi.SqlExceptionHelper logExceptions()-错误: wkb:未知字节顺序: 10101100
Java配置:
博士后:
@Configuration
public class UicDbConfig {
@Bean(name = "uicDataSource")
@Qualifier("uicDataSource")
@Primary
@ConfigurationProperties(prefix="spring.uic.datasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
对于蟑螂B:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "ru.stimul.geoservice.repo",
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager")
public class CockroachDbConfig {
@Autowired
private Environment env;
@Bean("primaryDataSource")
@ConfigurationProperties(prefix="spring.primary.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder
,@Qualifier("primaryDataSource") DataSource primaryDataSource){
LocalContainerEntityManagerFactoryBean em = builder
.dataSource(primaryDataSource)
.packages("ru.stimul.geoservice.domain.primary")
.persistenceUnit("domains")
.build();
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean(name = "primaryTransactionManager")
public JpaTransactionManager transactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory primaryEntityManagerFactory){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(primaryEntityManagerFactory);
return transactionManager;
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", env.getProperty("spring.primary.jpa.properties.hibernate.dialect"));
properties.setProperty("hibernate.show_sql", env.getProperty("spring.primary.jpa.show-sql"));
return properties;
}
}
应用yml:
spring:
application.name: geo-service
http:
encoding:
charset: UTF-8
enabled: true
main:
allow-bean-definition-overriding: true
primary:
datasource:
jdbcUrl: jdbc:postgresql://localhost:26257/geo_service
driverClassName: org.postgresql.Driver
username: admin
password:
jpa:
show-sql: true
hibernate:
ddl-auto: none
properties:
hibernate:
dialect: org.hibernate.spatial.dialect.cockroachdb.CockroachDB202SpatialDialect
generate-ddl: false
uic:
datasource:
jdbc-url: "jdbc:postgresql://localhost:2222/uic"
driverClassName: org.postgresql.Driver
username: postgres
password:
schema:
hikari:
connectionTimeout: 20000
maximumPoolSize: 10
max-active: 3
initial-size: 1
max-idle: 2
min-idle: 1
test-while-idle: true
test-on-borrow: true
max-idle-time: 160000
max-lifetime: 0
域对象:
import com.vividsolutions.jts.geom.Point;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import ru.stimul.geoservice.Geo;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "geo_object", schema = "geo_service")
@Slf4j
public class GeoObject {
@Id
@Column
String id;
@Column
Long created;
@Column(name = "version_id")
String versionId;
@Column(name = "hq_id")
String hqId;
@Column(name = "source_id")
String sourceId;
@Column(name = "geo_provider_id")
String geoProviderId;
@Column(name = "uic_id")
String uicId;
@Column(name = "address_id")
String addressId;
Point geometry;
@Column
String address;
@Column
String type;
@Column(name = "flat_count")
Long flatCount;
@Column(name = "voter_count")
Long voterCount;
@Column(name = "is_living")
boolean isLiving;
@Column(name = "is_generated")
boolean isGenerated;
@Column(length = 2000)
String payload;
}
存储库:
package ru.stimul.geoservice.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import ru.stimul.geoservice.domain.primary.GeoObject;
@Repository
public interface CockroachDbRepository extends JpaRepository<GeoObject, String> {
}
服务:
public GeoObject upsertWithoutAccessCheck(String requestId, GeoObject geoObject) {
if (Objects.isNull(geoObject.getId()) || StringUtils.isEmpty(geoObject.getId())) {
geoObject = geoObject.toBuilder()
.id(UUID.randomUUID().toString())
.build();
}
return cockroachDbRepository.saveAndFlush(geoObject);
}
波姆。xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.33.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>5.4.33.Final</version>
</dependency>
我建议您使用Hikari来处理多个数据源。
@Configuration
@ConfigurationProperties(prefix = "first.datasource")
@EnableTransactionManagement
public class FirstDBConfig extends HikariConfig {
@Bean
public DataSource firstDataSource() {
return new HikariDataSource(this);
}
...........................................
@Configuration
@ConfigurationProperties(prefix = "second.datasource")
@EnableTransactionManagement
@EnableJpaRepositories( basePackages = {
"com.myproyect.repos"
},
entityManagerFactoryRef = "myEntityManager",
transactionManagerRef = "myTransactionManager")
public class SecondDBConfig extends HikariConfig {
@Bean
public DataSource secondDataSource() {
return new HikariDataSource(this);
}
有关更多详细信息,请阅读hikari
https://github.com/brettwooldridge/HikariCP#readme
我正在使用spring数据jpa为我的服务实现多事务(数据库),带有两个持久的单元名。并在Jboss 6.4 EAP中部署 以下是我的服务详情 在Jboss中,我无法部署它,我遇到以下异常: 原因:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有com.test.demo.domain.repository.Tb
我正在尝试将每个@存储库与spring boot和Spring Data JPA的不同数据源连接起来。我使用了以下http://xantorohara.blogspot.com/2013/11/spring-boot-jdbc-with-multiple.html作为参考。这里是我正在使用的代码,试图使用Spring Data JPA实现类似的解决方案。 CustomerDBConfig.java
问题内容: 我必须在Hibernate,Jpa的应用程序(弹簧)中使用两个不同的数据库。我想直接将不同的表定义为不同的数据源。所以我使用两个不同的持久性单元,然后尝试使用 和 将不同的表放入不同的包中。但这不起作用。用第一个数据源来影响所有表。然后我尝试将诸如类的名称写入持久性XML文件 和it.two.app.domain.second.OtherTable 但是,当我运行Log时,说表’fir
我对spring boot很陌生,我想为我的项目创建一个多数据源。这是我现在的案子。我有两个多数据库实体包。比方说 因此,目前我有两个实体类 userh2.java 我希望实现这样的配置:如果我从UserMySql创建user,它将保存到MySql数据库;如果我从Userh2创建user,它将保存到H2数据库。所以,我也有两个DBConfig,假设MySqlDbConfig和h2dbconfig。
由于这个问题经常被问到,nutzcn社区累计了好几十个帖子,所以有必要单独开个文档描述一下 在一些复杂或遗留系统中,通常需要操作两个或更多的数据源. 总的原则: NutDao与DataSource是一对一关系. 值得注意的是, javax.sql.DataSource只是一个接口,而且NutDao只会调用其getConnection()方法. 配对模式在Ioc中的示例 你可以定义无数个dataSo
问题内容: 我有一个Spring + Hibernate + JPA应用程序。用户登录时,可以从要连接的数据库列表中进行选择(这是要求)。所有数据库都有相同的架构,因此将使用相同的实体和DAO。 现在,我有一个EntityManager(目前正在使用一个数据库),它被注入到DAO中,如下所示: 有什么方法可以让DAO根据从服务层接收到的参数/属性自动接收entityManager(由Spring管