1

Llevo ya unas semanas con un error que me trae de cabeza, a ver si alguien por aquí puede arrojar algo de luz...

Tengo una aplicación web construida mediante Spring Boot que se conecta a dos bases de datos, ambas Oracle.

En el entorno de desarrollo despliego en el tomcat embebido de Spring Boot y todo funciona bien. El problema ocurre cuando despliego en Weblogic (entorno de Integración) que, al intentar logearme en la aplicación, me devuelve la siguiente excepción cuando intenta ejecutar una select:

<Dec 27, 2019 1:58:16,335 PM CET> <Warning> <org.hibernate.engine.jdbc.spi.SqlExceptionHelper> <BEA-000000> <SQL Error: 30000, SQLState: 42Y07>
    <Dec 27, 2019 1:58:16,336 PM CET> <Error> <org.hibernate.engine.jdbc.spi.SqlExceptionHelper> <BEA-000000> <Schema 'SA' does not exist>
    <Dec 27, 2019 1:58:16,343 PM CET> <Error> <org.springframework.boot.web.servlet.support.ErrorPageFilter> <BEA-000000> <Forwarding to error page from request [/login_ldap] due to exception [could not prepare statement; SQL [select userentity0_.id as id1_6_, userentity0_.name as name2_6_, userentity0_.operator as operator3_6_, userentity0_.profile as profile4_6_, userentity0_.roles as roles5_6_, userentity0_.theme as theme6_6_, userentity0_.token as token7_6_ from USERS userentity0_ where userentity0_.name=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement]
    org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select userentity0_.id as id1_6_, userentity0_.name as name2_6_, userentity0_.operator as operator3_6_, userentity0_.profile as profile4_6_, userentity0_.roles as roles5_6_, userentity0_.theme as theme6_6_, userentity0_.token as token7_6_ from USERS userentity0_ where userentity0_.name=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:240)
        at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:223)
        at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
        at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
        ....
        ....
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
    Caused By: org.hibernate.exception.SQLGrammarException: could not prepare statement
        at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:63)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
        at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:181)
        ....
        ....
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
    Caused By: java.sql.SQLSyntaxErrorException: Schema 'SA' does not exist
        at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown Source)
        ....
        ....
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)
    Caused By: ERROR 42Y07: Schema 'SA' does not exist
        at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
        at org.apache.derby.iapi.error.StandardException.newException(Unknown Source)
        at org.apache.derby.impl.sql.catalog.DataDictionaryImpl.getSchemaDescriptor(Unknown Source)
        at org.apache.derby.iapi.sql.StatementUtil.getSchemaDescriptor(Unknown Source)
        ....
        ....
        at weblogic.work.ExecuteThread.execute(ExecuteThread.java:415)
        at weblogic.work.ExecuteThread.run(ExecuteThread.java:355)

He estado visitando muchos foros y en todos ellos hacen referencia a un error que puede ocurrir al usar Derby, y de hecho, en la traza de la excepción se puede ver org.apache.derby.iapi.error.StandardException.

El problema ha aparecido cuando he añadido el segundo datasource, ya que la configuración que traía Spring Boot por defecto no me valía y, por tanto, tuve que configurarlo "a mano".

Quizás tenga alguna configuración erronea, pero no consigo dar con ella.

Mi application.properties es:

spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@//IP1:PUERTO1/SERVICIO1
spring.datasource.jdbc-url=jdbc:oracle:thin:@//IP1:PUERTO1/SERVICIO1
spring.datasource.username=USERNAME1
spring.datasource.password=PASSWORD1
spring.datasource.hikari.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.hikari.connection-init-sql=ALTER SESSION SET CURRENT_SCHEMA=USERNAME1

spring.remd-datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.remd-datasource.url=jdbc:oracle:thin:@//IP2:PUERTO2/SERVICIO2
spring.remd-datasource.jdbc-url=jdbc:oracle:thin:@//IP2:PUERTO2/SERVICIO2
spring.remd-datasource.username=USERNAME2
spring.remd-datasource.password=PASSWORD2
spring.remd-datasource.hikari.driver-class-name=oracle.jdbc.OracleDriver
spring.remd-datasource.hikari.connection-init-sql=ALTER SESSION SET CURRENT_SCHEMA=USERNAME2

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

logging.level.org.springframework.web=ERROR
logging.level.org.springframework.security.access.intercept=debug
logging.level.org.springframework.security=ERROR
logging.level.com.hpe=DEBUG
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.file=./logs/serverApplication.log

server.error.whitelabel.enabled=false

Y las clases de configuración que he creado para los dos datasources:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "entityManagerFactory",
    basePackages = { "com.project.repository.clar" }
)
public class ClarDataSourceConfiguration {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    @Bean(name = "dataSource")
    public DataSource dataSource(DataSourceProperties properties) {
        return DataSourceBuilder.create(properties.getClassLoader())
                .type(HikariDataSource.class)
                .driverClassName(properties.determineDriverClassName())
                .url(properties.determineUrl())
                .username(properties.determineUsername())
                .password(properties.determinePassword())
                .build();
    }

    @Primary
    @Bean(name = "entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(
            EntityManagerFactoryBuilder builder, @Qualifier("dataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.project.model.clar").persistenceUnit("clar").build();
    }

    @Primary
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager(@Qualifier("entityManagerFactory") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}

y

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "remdEntityManagerFactory", 
    transactionManagerRef = "remdTransactionManager", 
    basePackages = { "com.project.repository.remd" }
)
public class RemdDataSourceConfiguration {

    @ConfigurationProperties(prefix = "spring.remd-datasource")
    @Bean(name = "remdDataSource")
    public DataSource dataSource(DataSourceProperties properties) {
        return DataSourceBuilder.create(properties.getClassLoader())
                .type(HikariDataSource.class)
                .driverClassName(properties.determineDriverClassName())
                .url(properties.determineUrl())
                .username(properties.determineUsername())
                .password(properties.determinePassword())
                .build();
    }

    @Bean(name = "remdEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean remdEntityManagerFactory(
            EntityManagerFactoryBuilder builder, @Qualifier("remdDataSource") DataSource remdDataSource) {
        return builder.dataSource(remdDataSource).packages("com.project.model.remd").persistenceUnit("remd").build();
    }

    @Bean(name = "remdTransactionManager")
    public PlatformTransactionManager remdTransactionManager(@Qualifier("remdEntityManagerFactory") EntityManagerFactory remdEntityManagerFactory) {
        return new JpaTransactionManager(remdEntityManagerFactory);
    }

}

Gracias a tod@s de antemano y un saludo!!

1 respuesta 1

1

He encontrado el problema.

Por alguna razón que no consigo entender, Spring no está cargando las propiedades definidas en el archivo application.properties que estoy intentando leer desde mi clase de configuración (properties.determineDriverClassName(), properties.determineUrl(), etc). En cambio, si hago un hardcode de los valores, todo funciona perfectamente.

Como solución alternativa, he definido estas propiedades en otro archivo de propiedades que ya estaba usando y todo funciona bien. No obstante, continuaré investigando por qué las propiedades de mi aplicación.propiedades no se están cargando.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.