我在一个多租户体系结构项目中工作,在这个项目中,我遇到了连接postgres数据库的问题。这是我收到的错误:
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:297) ~[postgresql-42.2.14.jar:42.2.14]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.14.jar:42.2.14]
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217) ~[postgresql-42.2.14.jar:42.2.14]
at org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.14.jar:42.2.14]
at org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.14.jar:42.2.14]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:358) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:477) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:560) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-3.4.5.jar:na]
at com.monytyz.billing.multitenancy.configuration.database.TenantConnectionProvider.getAnyConnection(TenantConnectionProvider.java:43) ~[classes/:an]
这是提供连接的类:
@Slf4j
@Primary
@Component
public class TenantConnectionProvider implements MultiTenantConnectionProvider {
@Autowired
private TenantSchemaResolver tenantSchemaResolver;
@Override
public Connection getAnyConnection() throws SQLException {
DataSource dataSource = createDataSource();
return dataSource.getConnection();
}
@Override
public void releaseAnyConnection(Connection connection) throws SQLException {
connection.close();
}
@Override
public Connection getConnection(String tenantIdentifier) throws SQLException {
String currentTenantSchema = tenantSchemaResolver.resolveCurrentTenantIdentifier();
if (!currentTenantSchema.equalsIgnoreCase(tenantIdentifier)) {
tenantIdentifier = currentTenantSchema;
log.info("Schemas are not same ... ");
}
log.info("Get connection for tenant {}", tenantIdentifier);
final Connection connection = getAnyConnection();
connection.setSchema(tenantIdentifier);
return connection;
}
@Override
public void releaseConnection(String tenantIdentifier, Connection connection) throws SQLException {
log.info("Release connection for tenant {}", tenantIdentifier);
connection.setSchema(MultiTenantConstants.MASTER_DB_SCHEMA);
releaseAnyConnection(connection);
}
public DataSource createDataSource() {
DataSource dataSource = DataSourceBuilder
.create().driverClassName("org.postgresql.Driver")
.username("postgres")
.password("postgres")
.url("jdbc:postgresql://postgres:5432/billing")
.build();
return dataSource;
}
@Override
public boolean supportsAggressiveRelease() {
return false;
}
@Override
public boolean isUnwrappableAs(Class aClass) {
return false;
}
@Override
public <T> T unwrap(Class<T> aClass) {
return null;
}
}
配置类:
@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig {
@Autowired
private JpaProperties jpaProperties;
@Autowired
private TenantConnectionProvider multiTenantConnection;
@Autowired
private CurrentTenantIdentifierResolver currentTenantIdentifier;
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
return new HibernateJpaVendorAdapter();
}
@Bean
@Scope(
value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,
proxyMode = ScopedProxyMode.TARGET_CLASS
)
public JdbcTemplate jdbcTemplate() {
DataSource dataSource = getDataSource();
return new JdbcTemplate(dataSource);
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
Map<String, Object> jpaPropertiesMap = createJpaPropertiesMap();
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
DataSource dataSource = getDataSource();
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan("com.monytyz.billing*");
factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
factoryBean.setJpaPropertyMap(jpaPropertiesMap);
return factoryBean;
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
private Map<String, Object> createJpaPropertiesMap() {
Map<String, Object> jpaPropertiesMap = new HashMap<>(jpaProperties.getProperties());
jpaPropertiesMap.put(Environment.MULTI_TENANT, MultiTenancyStrategy.SCHEMA);
// jpaPropertiesMap.put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
jpaPropertiesMap.put(Environment.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnection);
jpaPropertiesMap.put(Environment.MULTI_TENANT_IDENTIFIER_RESOLVER, currentTenantIdentifier);
jpaPropertiesMap.put(Environment.FORMAT_SQL, true);
jpaPropertiesMap.put(Environment.SHOW_SQL, true);
return jpaPropertiesMap;
}
private DataSource getDataSource() {
return multiTenantConnection.createDataSource();
}
}
这是docker-compose.yml文件:
version: "3"
services:
postgres:
image: postgres
network_mode: bridge
container_name: postgres
volumes:
- postgres-data:/var/lib/postgresql/data
expose:
- 5432
ports:
- 5432:5432
environment:
POSTGRES_DB: billing
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
restart: unless-stopped
billing:
build:
context: ./
dockerfile: Dockerfile
image: monytyz/billing
network_mode: bridge
container_name: billing
expose:
- 8080
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- postgres
links:
- postgres
volumes:
postgres-data:
为什么会出现postgres连接错误,如何解决?
暂无答案!
目前还没有任何答案,快来回答吧!