refactor: optimize code for pull request 249,fix issue #153

This commit is contained in:
Echo009 2021-04-03 17:08:16 +08:00
parent 5f0865129e
commit a0f76f7ba9
5 changed files with 45 additions and 48 deletions

View File

@ -9,15 +9,13 @@ import java.util.*;
/**
* 多重数据源配置
*
* @author tjq
* @author Kung Yao
* @since 2020/4/27
*/
@Component
@ConfigurationProperties("spring.datasource")
public class MultiDatasourceProperties {
private DataSourceProperties remote = new DataSourceProperties();
private DataSourceProperties local = new DataSourceProperties();
@ -27,7 +25,7 @@ public class MultiDatasourceProperties {
private HibernateProperties hibernate = new HibernateProperties();
public void setHibernate( HibernateProperties hibernate ) {
public void setHibernate(HibernateProperties hibernate) {
this.hibernate = hibernate;
}
@ -39,9 +37,9 @@ public class MultiDatasourceProperties {
public static class HibernateProperties {
private Map<String,String> properties = Maps.newHashMap();
private Map<String, String> properties = Maps.newHashMap();
public void setProperties( Map<String, String> properties ) {
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
@ -50,11 +48,11 @@ public class MultiDatasourceProperties {
}
}
public void setLocal( DataSourceProperties local ) {
public void setLocal(DataSourceProperties local) {
this.local = local;
}
public void setRemote( DataSourceProperties remote ) {
public void setRemote(DataSourceProperties remote) {
this.remote = remote;
}

View File

@ -1,23 +0,0 @@
package tech.powerjob.server.persistence.config;
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
* 2021/3/24 下午 04:23
* 1074_King
*/
public class PowerJobPGDialect extends PostgreSQL10Dialect {
@Override
public SqlTypeDescriptor remapSqlTypeDescriptor( SqlTypeDescriptor sqlTypeDescriptor ) {
if ( Types.CLOB == sqlTypeDescriptor.getSqlType() ) {
return LongVarcharTypeDescriptor.INSTANCE;
}
return super.remapSqlTypeDescriptor( sqlTypeDescriptor );
}
}

View File

@ -1,8 +1,5 @@
package tech.powerjob.server.persistence.config;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
@ -10,7 +7,6 @@ import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
@ -46,12 +42,10 @@ public class RemoteJpaConfig {
@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
@ -64,10 +58,6 @@ public class RemoteJpaConfig {
jpaProperties.setOpenInView(false);
jpaProperties.setShowSql(false);
HibernateProperties hibernateProperties = new HibernateProperties();
hibernateProperties.setDdlAuto("update");
@ -80,11 +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() );
Map<String, Object> datasourceProperties = genDatasourceProperties();
datasourceProperties.putAll(properties.getRemote().getHibernate().getProperties());
return builder
.dataSource(omsRemoteDatasource)
.properties( datasourceProperties )
.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

@ -8,8 +8,10 @@ spring.datasource.core.username=root
spring.datasource.core.password=No1Bug2Please3!
spring.datasource.core.hikari.maximum-pool-size=20
spring.datasource.core.hikari.minimum-idle=5
## The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.datasource.remote.hibernate.properties.hibernate.dialect=tech.powerjob.server.persistence.config.PowerJobPGDialect
####### 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 #######