JpaContainerEntityManagerFactoryBean.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.primeton.dgs.kernel.core.dao.jpa;
  2. import com.primeton.dgs.kernel.core.util.MetadataWebContext;
  3. import com.primeton.dgs.kernel.core.web.springframework.DatabaseRecognizer;
  4. import org.apache.commons.lang.StringUtils;
  5. import org.springframework.beans.factory.InitializingBean;
  6. import org.springframework.context.ApplicationContextException;
  7. import org.springframework.core.io.Resource;
  8. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  9. import org.springframework.core.io.support.ResourcePatternResolver;
  10. import org.springframework.orm.jpa.JpaVendorAdapter;
  11. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  12. import org.springframework.orm.jpa.vendor.Database;
  13. import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  14. import javax.persistence.PersistenceException;
  15. import javax.sql.DataSource;
  16. import java.io.InputStream;
  17. import java.sql.Connection;
  18. import java.sql.DatabaseMetaData;
  19. import java.sql.SQLException;
  20. import java.util.Arrays;
  21. import java.util.Map;
  22. import java.util.Properties;
  23. /**
  24. *
  25. * JPA 配置
  26. *
  27. * <pre>
  28. *
  29. * Created by zhaopx.
  30. * User: zhaopx
  31. * Date: 2020/9/1
  32. * Time: 17:44
  33. *
  34. * </pre>
  35. *
  36. * @author zhaopx
  37. */
  38. public class JpaContainerEntityManagerFactoryBean
  39. extends LocalContainerEntityManagerFactoryBean
  40. implements InitializingBean {
  41. private DatabaseRecognizer databaseRecognizer;
  42. @Override
  43. public void afterPropertiesSet() throws PersistenceException {
  44. // 扫描配置文件
  45. // 首先根据数据库选择配置文件
  46. String databaseName = MetadataWebContext.getInstance().getDatabaseName();
  47. Properties props = new Properties();
  48. String configFile = "classpath*:".concat("/spring/hibernate_").concat(databaseName.toLowerCase()).concat("_local.properties");
  49. try {
  50. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  51. Resource[] resources = resolver.getResources(configFile);
  52. for (Resource resource : resources) {
  53. try(InputStream inputStream = resource.getInputStream()) {
  54. Properties tmp = new Properties();
  55. tmp.load(inputStream);
  56. props.putAll(tmp);
  57. }
  58. }
  59. } catch (Exception e) {
  60. logger.error("无法读取配置文件: " + configFile, e);
  61. }
  62. if(!props.isEmpty()) {
  63. for (Map.Entry<Object, Object> entry : props.entrySet()) {
  64. getJpaPropertyMap().put((String) entry.getKey(), entry.getValue());
  65. }
  66. }
  67. Database database;
  68. try {
  69. database = Database.valueOf(databaseName.toUpperCase());
  70. } catch (Exception e) {
  71. logger.error("unkown db " + databaseName + " use Database.DEFAULT, please set spring bean databaseRecognizer key-value, to match " + Arrays.asList(Database.values()));
  72. database = Database.DEFAULT;
  73. }
  74. final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
  75. jpaVendorAdapter.setDatabasePlatform((String) getJpaPropertyMap().get("hibernate.dialect"));
  76. jpaVendorAdapter.setDatabase(database);
  77. jpaVendorAdapter.setGenerateDdl(false);
  78. jpaVendorAdapter.setShowSql(true);
  79. setJpaVendorAdapter(jpaVendorAdapter);
  80. super.afterPropertiesSet();
  81. }
  82. @Override
  83. public void setDataSource(DataSource dataSource) {
  84. super.setDataSource(dataSource);
  85. // DB Type
  86. if(StringUtils.isBlank(MetadataWebContext.getInstance().getDatabaseName())) {
  87. try (Connection conn = dataSource.getConnection()) {
  88. DatabaseMetaData meta = conn.getMetaData();
  89. String databaseName = meta.getDatabaseProductName();
  90. String productVersion = meta.getDatabaseProductVersion();
  91. if (logger.isInfoEnabled()) {
  92. logger.info("Current Database is " + databaseName + ", Version is " + productVersion);
  93. }
  94. databaseName = recognizeDatabaseName(databaseName, productVersion);
  95. // 将数据库名缓存起来
  96. MetadataWebContext.getInstance().setDatabaseName(databaseName);
  97. } catch (SQLException e) {
  98. String s = "Cannot determine the database brand name for loading ApplicationContext";
  99. logger.error(s, e);
  100. throw new ApplicationContextException(s, e);
  101. }
  102. }
  103. }
  104. /**
  105. * 识别数据库名称,已经配置在spring/corebean/context-base.xml文件中
  106. * @param databaseName 从数据库连接获取的数据库名称
  107. * @param productVersion 数据库版本
  108. * @return 转换成系统需要的名称
  109. */
  110. private String recognizeDatabaseName(String databaseName, String productVersion) {
  111. return databaseRecognizer.getDatabaseName(databaseName);
  112. }
  113. public void setDatabaseRecognizer(DatabaseRecognizer databaseRecognizer) {
  114. this.databaseRecognizer = databaseRecognizer;
  115. }
  116. }