Google Cloud SQL + JPA without WTP

751 views Asked by At

I've managed to connect to my Google Cloud SQL database from Java. Currently I've been verifying this by going like this:

Connection conn = DriverManager.getConnection(getUrl());

Then retrieving a prepared statement from that and so forth. getUrl() is a method that returns the url in the format that Google are specifying: "jdbc:google:mysql://xxx/xxx?user=xxx&password=xxx".

I would like to use JPA with my database. Looking at Google's documentation for this, they seem to recommend WTP a.k.a. Eclipse Web Tools Platform (https://developers.google.com/eclipse/docs/cloudsql-jpatools). I don't use Eclipse and I would really like to avoid using this tool for something that normally should be very easy to achieve without any gui/wizard/plugin-thingy.

My Java servlet is written in Spring and my app config looks something like this:

@EnableWebMvc
@EnableScheduling
@EnableTransactionManagement
@EnableJpaRepositories("stuff")
@ComponentScan("stuff")
@Configuration
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource driver = new DriverManagerDataSource();
        driver.setDriverClassName("org.postgresql.Driver"); //Obviously not working for Google Cloud SQL
        driver.setUrl(Config.getUrl()); // Same url as described above.
        driver.setUsername(/*omitted*/);
        driver.setPassword(/*omitted*/);
        return driver;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager txManager = new JpaTransactionManager();
        txManager.setEntityManagerFactory(entityManagerFactory());
        return txManager;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true);
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("stuff");
        factory.setPersistenceUnitName("PU");
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();
        return factory.getObject();
    }
}

My persistence.xml is looking something like this:

<?xml version="1.0" encoding="UTF-8"?>

<persistence
        xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">

    <persistence-unit name="PU">
        <class>stuff.Message</class>

        <properties>
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
        </properties>

    </persistence-unit>

</persistence>

Is there any way that I could modify my AppConfig.java+persistence.xml to make this work with Google Cloud SQL? As mentioned I would love skipping the Eclipse Plugin.

1

There are 1 answers

0
Patrice Blanchardie On BEST ANSWER

If you are using AppEngine, you might want to read https://cloud.google.com/sql/docs/app-engine-connect and https://cloud.google.com/appengine/docs/java/cloud-sql/.

Here, it seems you're trying to connect to Google Cloud SQL instance without using the Google Cloud SQL Driver, but with a Google connection string which requires this driver, as explained in their sample:

// Load the class that provides the new "jdbc:google:mysql://" prefix.
Class.forName("com.mysql.jdbc.GoogleDriver");

So your connection string is not recognised (and the PostgreSQL driver doesn't help).

If you're not using AppEngine, switch to a MySQL driver and remove "google" in your connection string, as you would do with a standard MySQL connection (with host, port, etc. instead of instance name).