Spring Boot + Hibernate + Google Cloud Sql + App Engine

Asked

Viewed 234 times

1

I’m having problems configuring the connection in my application with Google Cloud Sql (using the technologies highlighted in the title). I wish I could allow Spring to be responsible for Datasource, Entitymanagerfactory and Transaction.

So I set up the following class and it works well when I am running the application locally, ie passing settings to connect to my local Mysql database. However, when changing the Datasource configuration to connect to Google Cloud Sql, make the error dirty:

Error:

java.lang.Classnotfoundexception: com.mysql.jdbc.Googledriver

Follow my files:

Setup

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
public class BancoDeDadosConfig {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private LocalContainerEntityManagerFactoryBean entityManagerFactory;

    @Bean
    public DataSource dataSource() throws ClassNotFoundException {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.GoogleDriver");
        dataSource.setUrl("jdbc:google:mysql://urlMeuBancoNaNuvem:us-central1:meuBanco/nomeDaDatabase");
        dataSource.setUsername("meuUsuario");
        dataSource.setPassword("minhaSenha");

        return dataSource;
    }

    /**
     * Declarando entidate manager factory do JPA
     */

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

        entityManagerFactory.setDataSource(dataSource);

        // escaneando pacote atras de classes anotadas
        entityManagerFactory.setPackagesToScan(" ... meuPacote .. ");

        //vendor
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactory.setJpaVendorAdapter(vendorAdapter);

        //propriedades do Hibernate
        Properties propriedades = new Properties();
        propriedades.put("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect");
        propriedades.put("hibernate.show_sql", "true");
        propriedades.put("hibernate.hbm2ddl.auto", "update");

        entityManagerFactory.setJpaProperties(propriedades);

        return entityManagerFactory;
    }

    /**
     * Transação
     */
    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory.getObject());

        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }
}

appengine-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

    <version>1</version>
    <threadsafe>true</threadsafe>
    <runtime>java8</runtime>

    <use-google-connector-j>true</use-google-connector-j>

    <system-properties>
        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
    </system-properties>

</appengine-web-app>

application properties

arquivo vazio

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.meuProjeto</groupId>
<artifactId>App</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>App</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>

    <appengine.sdk.version>1.9.63</appengine.sdk.version>
</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-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </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-security</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-1.0-sdk</artifactId>
        <version>${appengine.sdk.version}</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jul-to-slf4j</artifactId>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.6</version>
    </dependency>

    <dependency>
        <groupId>com.google.cloud.sql</groupId>
        <artifactId>mysql-socket-factory-connector-j-6</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.2.8.Final</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <!--configuração para execução local-->
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.3.1</version>
        </plugin>

        <!--configuração para depuração local-->
        <!--<plugin>-->
            <!--<groupId>com.google.appengine</groupId>-->
            <!--<artifactId>appengine-maven-plugin</artifactId>-->
            <!--<version>${appengine.sdk.version}</version>-->
            <!--<configuration>-->
                <!--<jvmFlags>-->
                    <!--<jvmFlag>-Xdebug</jvmFlag>-->
                    <!--<jvmFlag>-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=n</jvmFlag>-->
                <!--</jvmFlags>-->
            <!--</configuration>-->
        <!--</plugin>-->

    </plugins>
 </build>
</project>

Could someone help me?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.