Search code examples
javaspring-boothibernatejpa

Failed to initialize JPA File Must Exist Error In Kubernetes


I am trying to run an application with two different datasources. One of the datasource is to write one of them is to read

My application runs smoothly on my local. Not a single error. But when I deploy it to the k8s cluster, I am getting this error.

Log from K8S

Failed to initialize JPA EntityManagerFactory: File must exist : /proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/1/cwd/proc/self/task/7/cwd/proc/sys/net/ipv4/tcp_autocorking

I literally have no clue. The logs below is from my local. I think its related with JPA EntityManagerFactory so I believe it worth to share.

Log from Local

Building JPA container EntityManagerFactory for persistence unit 'default'

PersistenceUnitInfo [
    name: default
    persistence provider classname: null
    classloader: org.springframework.boot.loader.LaunchedURLClassLoader@8bd1b6a
    excludeUnlistedClasses: true
    JTA datasource: null
    Non JTA datasource: HikariDataSource (null)
    Transaction type: RESOURCE_LOCAL
    PU root URL: file:/Users/berkin.akaydin/IdeaProjects/sendingmethods-api/target/sendingmethods-api-XXX.XXX.jar
    Shared Cache Mode: UNSPECIFIED
    Validation Mode: AUTO
    Jar files URLs []
    Managed classes names [
        com.berkin.sendingmethodsapi.domain.connection.persistance.model.write.OrderEntity
        com.berkin.sendingmethodsapi.domain.connection.persistance.model.write.TbbLogEntity
        com.berkin.sendingmethodsapi.domain.connection.persistance.model.write.WriteConnectionEntity
        ]
    Mapping files names []
    Properties []
]

Read Configuration (the same for write)

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    basePackages = ["com.berkin.sendingmethodsapi.domain.connection.persistance.repository.read"],
    entityManagerFactoryRef = "readEntityManagerFactory",
    transactionManagerRef = "readTransactionManager"
)
class readConfiguration {

    @Bean("readDataSourceProperties")
    @ConfigurationProperties("spring.datasource.read")
    fun readDataSourceProperties() = DataSourceProperties()

    @Bean("readDataSource")
    fun readDataSource(
        @Qualifier("readDataSourceProperties") readDataSourceProperties: DataSourceProperties
    ): DataSource =
        readDataSourceProperties.initializeDataSourceBuilder().build()

    @Bean("readTransactionManager")
    fun readTransactionManager(
        @Qualifier("readEntityManagerFactory") readEntityManagerFactory: LocalContainerEntityManagerFactoryBean
    ) = readEntityManagerFactory.`object`?.let { JpaTransactionManager(it) }

    @Bean("readEntityManagerFactory")
    fun readEntityManagerFactory(
        @Qualifier("readDataSource") dataSource: DataSource
    ): LocalContainerEntityManagerFactoryBean {
        val entityManagerFactory = LocalContainerEntityManagerFactoryBean()
        entityManagerFactory.dataSource = dataSource
        entityManagerFactory.setPackagesToScan("com.berkin.sendingmethodsapi.domain.connection.persistance.model.read")
        entityManagerFactory.persistenceProvider = HibernatePersistenceProvider()
        entityManagerFactory.jpaVendorAdapter = HibernateJpaVendorAdapter()
        return entityManagerFactory
    }
}

application.properties

jpa:
    open-in-view: false
    hibernate.ddl-auto: validate
    database: MySQL

Dockerfile

FROM amazoncorretto:17-alpine as builder
ARG JAR_FILE=*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM amazoncorretto:17-alpine
COPY --from=builder dependencies/ ./
COPY --from=builder snapshot-dependencies/ ./
COPY --from=builder spring-boot-loader/ ./
COPY --from=builder application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]

Edit: Full log is here https://pastebin.com/smhGekNb


Solution

  • I have fixed the problem.

    I don't know the exact cause of the problem but here is the solution. I want to thank @SebPerb because of his suggestion.

    When I use WORKDIR, the problem is fixed.

    FROM amazoncorretto:17-alpine AS builder
    WORKDIR application
    ARG JAR_FILE=*.jar
    COPY ${JAR_FILE} application.jar
    RUN java -Djarmode=layertools -jar application.jar extract
    
    FROM amazoncorretto:17-alpine
    WORKDIR application
    COPY --from=builder application/dependencies/ ./
    COPY --from=builder application/snapshot-dependencies/ ./
    COPY --from=builder application/spring-boot-loader/ ./
    COPY --from=builder application/application/ ./
    ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]