fix: compatibility issues for PostgreSQL

This commit is contained in:
tjq 2021-04-05 12:52:48 +08:00
commit 12d0d4dbb0
6 changed files with 115 additions and 3 deletions

View File

@ -0,0 +1,66 @@
package tech.powerjob.server.persistence.config;
import com.google.common.collect.Maps;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.*;
/**
* 多重数据源配置
*
* @author Kung Yao
* @since 2020/4/27
*/
@Component
@ConfigurationProperties("spring.datasource")
public class MultiDatasourceProperties {
private DataSourceProperties remote = new DataSourceProperties();
private DataSourceProperties local = new DataSourceProperties();
public static class DataSourceProperties {
private HibernateProperties hibernate = new HibernateProperties();
public void setHibernate(HibernateProperties hibernate) {
this.hibernate = hibernate;
}
public HibernateProperties getHibernate() {
return hibernate;
}
}
public static class HibernateProperties {
private Map<String, String> properties = Maps.newHashMap();
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public Map<String, String> getProperties() {
return properties;
}
}
public void setLocal(DataSourceProperties local) {
this.local = local;
}
public void setRemote(DataSourceProperties remote) {
this.remote = remote;
}
public DataSourceProperties getLocal() {
return local;
}
public DataSourceProperties getRemote() {
return remote;
}
}

View File

@ -39,10 +39,13 @@ public class RemoteJpaConfig {
@Resource(name = "omsRemoteDatasource")
private DataSource omsRemoteDatasource;
@Resource(name = "multiDatasourceProperties")
private MultiDatasourceProperties properties;
public static final String CORE_PACKAGES = "tech.powerjob.server.persistence.remote";
/**
* 生成配置文件包括 JPA配置文件和Hibernate配置文件相当于下三个配置
* 生成配置文件包括 JPA配置文件和Hibernate配置文件相当于下三个配置
* spring.jpa.show-sql=false
* spring.jpa.open-in-view=false
* spring.jpa.hibernate.ddl-auto=update
@ -67,10 +70,11 @@ public class RemoteJpaConfig {
@Primary
@Bean(name = "remoteEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean initRemoteEntityManagerFactory(EntityManagerFactoryBuilder builder) {
Map<String, Object> datasourceProperties = genDatasourceProperties();
datasourceProperties.putAll(properties.getRemote().getHibernate().getProperties());
return builder
.dataSource(omsRemoteDatasource)
.properties(genDatasourceProperties())
.properties(datasourceProperties)
.packages(CORE_PACKAGES)
.persistenceUnit("remotePersistenceUnit")
.build();

View File

@ -0,0 +1,30 @@
package tech.powerjob.server.persistence.config.dialect;
import org.hibernate.dialect.PostgreSQL10Dialect;
import org.hibernate.type.descriptor.sql.LongVarcharTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import java.sql.Types;
/**
* @author Kung Yao
* @author Echo009
* 2021/3/24 下午 04:23
* 1074_King
*/
public class PowerJobPGDialect extends PostgreSQL10Dialect {
/**
* 使用 {@link Types#LONGVARCHAR} 覆盖 {@link Types#CLOB} 类型
*
* 注意如果在 PG 库创建表时使用的列类型为 oid 那么这样会导致没法正确读取数据
* PowerJob 中能这样用是因为 PowerJob 的所有实体类中被 @Lob 注解标记的列对应数据库中的字段类型都是 text
* 另外还需要注意数据库版本如果是 10.x 以前的需自行提供一个合适的 Dialect 选择合适的版本继承
*
* 更多内容请关注该 issueshttps://github.com/PowerJob/PowerJob/issues/153
*/
@Override
public SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) {
return Types.CLOB == sqlCode ? LongVarcharTypeDescriptor.INSTANCE : null;
}
}

View File

@ -9,6 +9,10 @@ spring.datasource.core.password=No1Bug2Please3!
spring.datasource.core.hikari.maximum-pool-size=20
spring.datasource.core.hikari.minimum-idle=5
####### Fix issue 153 for PostgreSQL users, see https://github.com/PowerJob/PowerJob/issues/153#issuecomment-812771783 for more information #######
####### Notice, the configuration below only supports PostgreSQL 10.x and later #######
# spring.datasource.remote.hibernate.properties.hibernate.dialect=tech.powerjob.server.persistence.config.dialect.PowerJobPGDialect
####### MongoDB properties(Non-core configuration properties) #######
####### configure oms.mongodb.enable=false to disable mongodb #######
oms.mongodb.enable=true

View File

@ -9,6 +9,10 @@ spring.datasource.core.password=No1Bug2Please3!
spring.datasource.core.hikari.maximum-pool-size=20
spring.datasource.core.hikari.minimum-idle=5
####### Fix issue 153 for PostgreSQL users, see https://github.com/PowerJob/PowerJob/issues/153#issuecomment-812771783 for more information #######
####### Notice, the configuration below only supports PostgreSQL 10.x and later #######
# spring.datasource.remote.hibernate.properties.hibernate.dialect=tech.powerjob.server.persistence.config.dialect.PowerJobPGDialect
####### MongoDB properties(Non-core configuration properties) #######
####### configure oms.mongodb.enable=false to disable mongodb #######
oms.mongodb.enable=true

View File

@ -9,6 +9,10 @@ spring.datasource.core.password=No1Bug2Please3!
spring.datasource.core.hikari.maximum-pool-size=20
spring.datasource.core.hikari.minimum-idle=5
####### Fix issue 153 for PostgreSQL users, see https://github.com/PowerJob/PowerJob/issues/153#issuecomment-812771783 for more information #######
####### Notice, the configuration below only supports PostgreSQL 10.x and later #######
# spring.datasource.remote.hibernate.properties.hibernate.dialect=tech.powerjob.server.persistence.config.dialect.PowerJobPGDialect
####### MongoDB properties(Non-core configuration properties) #######
####### configure oms.mongodb.enable=false to disable mongodb #######
oms.mongodb.enable=true