Fala Devs, sei que existem diversos tópicos quanto a este assunto no fórum, mas nenhum me ajudou, tenho uma aplicação spring-boot acessando dois bancos distintos, um MS SQL Server 2019 (Principal) e um PostgreSQL 10.
Não sei o que ocorreu para que acontecesse este problema, anteriormente o sistema estava funcionando normalmente com as configurações atuais, eu estava utilizando a versão 2.6.8 do spring-boot, depois que apresentou esse erro, atualizei para 2.7.6, mas o erro persiste.
Unable to acquire JDBC Connection [n/a] at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions:126
java.sql.SQLFeatureNotSupportedException: null
...
Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection at br.unb.webservices.db.mssql.service.OauthAccessTokenService.validarToken:58
org.springframework.orm.jpa.JpaSystemException: Unable to acquire JDBC Connection; nested exception is org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
...
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
Seguem trechos do meu código:
MSSqlDataSourceConfig
@Configuration/* Register this class as a Spring component. */
@EnableTransactionManagement/* Enable annotation-based transaction management. */
@EnableJpaRepositories(/* Teaches Spring how to create connections and where seek for repositories for this database. */
entityManagerFactoryRef = "mssqlEntityManagerFactory", // Specifies which @Bean will provide the EntityManager
transactionManagerRef = "mssqlTransactionManager", // Specifies which @Bean will the provide JpaTransactionManager
basePackages = "br.unb.webservices.db.mssql.repository") // Teaches Spring where to seek for repositories for this database
public class MSSqlDataSourceConfig {
/**
* Creates a new DataSource which will be used to create a
* {@link LocalContainerEntityManagerFactoryBean} and teaches Spring how to
* read the configurations from application.properties.
*
* @return Created custom DataSource
*/
@Primary/* Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value */
@Bean(name = "mssqlDataSource")
@ConfigurationProperties(prefix = "mssql.datasource")/* All configurations with this prefix in application.properties will be related to this database. */
public DataSource mssqlDataSource() {
return DataSourceBuilder.create().build();
}
/**
* Provides a {@link EntityManager} for this database.
*
* @param builder will be injected by Spring.
* @param dataSource inject the bean provided by mssqlDataSource().
*
* @return a factory of EntityManager.
*/
@Primary/* Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value */
@Bean(name = "mssqlEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean mssqlEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("mssqlDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("br.unb.webservices.db.mssql.model")
.persistenceUnit("mssqlPU")
.build();
}
/**
* Provides a transaction manager.
*
* @param entityManagerFactory inject the
* {@link LocalContainerEntityManagerFactoryBean} provided by
* mssqlEntityManagerFactory()
*
* @return a transaction manager.
*/
@Primary/* Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value */
@Bean(name = "mssqlTransactionManager")
public PlatformTransactionManager mssqlTransactionManager(@Qualifier("mssqlEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
PostgresDataSourceConfig
@Configuration/* Register this class as a Spring component. */
@EnableTransactionManagement/* Enable annotation-based transaction management. */
@EnableJpaRepositories(/* Teaches Spring how to create connections and where seek for repositories for this database. */
entityManagerFactoryRef = "postgresqlEntityManagerFactory", // Specifies which @Bean will provide the EntityManager
transactionManagerRef = "postgresqlTransactionManager", // Specifies which @Bean will the provide JpaTransactionManager
basePackages = "br.unb.webservices.db.postgres.repository") // Teaches Spring where to seek for repositories for this database
public class PostgresDataSourceConfig {
/**
* Creates a new DataSource which will be used to create a
* {@link LocalContainerEntityManagerFactoryBean} and teaches Spring how to
* read the configurations from application.properties.
*
* @return
*/
@Bean(name = "postgresqlDataSource")/* Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value */
@ConfigurationProperties(prefix = "postgresql.datasource")/* All configurations with this prefix in application.properties will be related to this database. */
public DataSource postgresqlDataSource() {
return DataSourceBuilder.create().build();
}
/**
* Provides a {@link EntityManager} for this database.
*
* @param builder will be injected by Spring.
* @param dataSource inject the bean provided by mssqlDataSource().
*
* @return a factory of EntityManager.
*
*/
@Bean(name = "postgresqlEntityManagerFactory")/* Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value */
public LocalContainerEntityManagerFactoryBean postgresqlEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("postgresqlDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("br.unb.webservices.db.postgres.model")
.persistenceUnit("postgresPU")
.build();
}
/**
* Provides a transaction manager.
*
* @param entityManagerFactory inject the
* {@link LocalContainerEntityManagerFactoryBean} provided by
* mssqlEntityManagerFactory()
*
* @return a transaction manager.
*
*/
@Bean(name = "postgresqlTransactionManager")/* Indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value */
public PlatformTransactionManager postgresqlTransactionManager(@Qualifier("postgresqlEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="mssqlPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<shared-cache-mode>NONE</shared-cache-mode>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://xxx.xxx.xxx.xxx:xxxx;databaseName=BDPessoa"/>
<property name="javax.persistence.jdbc.user" value="xxxx"/>
<property name="javax.persistence.jdbc.password" value="xxxx"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect"/>
</properties>
</persistence-unit>
<persistence-unit name="postgresPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<shared-cache-mode>NONE</shared-cache-mode>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://xxx.xxx.xxx.xxx:xxxx/administrativo"/>
<property name="javax.persistence.jdbc.user" value="xxxx"/>
<property name="javax.persistence.jdbc.password" value="xxxx"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
application.properties
#JPA
spring.jpa.show-sql=true
spring.jpa.open-in-view=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.proc.param_null_passing=true
#Database MS SQL Server
mssql.datasource.jdbcUrl=jdbc:sqlserver://xxx.xxx.xxx.xxx:xxxx;databaseName=BDPessoa;integratedSecurity=false;encrypt=true;trustServerCertificate=true
mssql.datasource.username=xxxx
mssql.datasource.password=xxxx
#Database PostreSQL Server
postgresql.datasource.jdbcUrl=jdbc:postgresql://xxx.xxx.xxx.xxx:xxxx/administrativo
postgresql.datasource.username=xxxx
postgresql.datasource.password=xxxx
#Encoding
spring.messages.encoding=UTF-8
server.servlet.encoding.charset=UTF-8
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/>
</parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>...</name>
<description>API de gestão de cadastros de Pessoas Física e Jurídica.</description>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Apesar de uma classe ser apresentada no log, essa classe não possui erros, como disse anteriormente, estava funcionando normalmente, não houve alteração nem na classe, nem no banco de dados.
Aparentemente, é algum problema no driver de conexão, mas não consigo entender o que houve, e porque parou de funcionar…
Estou usando a JDK 17, Spring-boot 2.7.6, IDE NetBeans 15, SQL Server 2019, PostgreSQL 10
Desde já agradeço a ajuda de todos.