David McCullough
David McCullough

Reputation: 1

Using GCP Cloud SQL SocketFactory with Hibernate Configuration

I am trying to connect to a Cloud SQL Postgres Database instance from GCP App Engine (GAE) Standard. Using the database instance connection name (my-project-id:europe-west1:db-instance-name) with the Cloud SQL SocketFactory with Hibernate.

    public static SessionFactory getSessionFactory()
    {
        if (sessionFactory == null) {
            try {
                Configuration configuration = new Configuration();

                // Hibernate settings equivalent to hibernate.cfg.xml's properties
                Properties settings = new Properties();

                settings.put("hibernate.connection.provider_class","com.google.cloud.sql.postgres.SocketFactory");

                settings.put("hibernate.hikari.minimumIdle","5");
                settings.put("hibernate.hikari.maximumPoolSize","10");
                settings.put("hibernate.hikari.idleTimeout","30000");
                 settings.put("hibernate.hikari.dataSource.url","jdbc:postgres://google/database-name?cloudSqlInstance=project:europe-west1:instance-id&socketFactory=com.google.cloud.sql.postgres.SocketFactory");
                settings.put("hibernate.hikari.dataSource.user","postgres");
                settings.put("hibernate.hikari.dataSource.password","password");                    


                      settings.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
        configuration.addAnnotatedClass(Country.class);

                ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            } catch (Exception e) {
                LOGGER.info("============EXCEPTION===========>"+e.getMessage());
            System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        return sessionFactory;

Unable to create requested service [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]

Upvotes: 0

Views: 664

Answers (1)

Ricardo
Ricardo

Reputation: 166

I think that the implementation you’re using isn’t fit for App Engine Standard, you should check the Google implementation that can be found on the official documentation.

Official Google documentation to connect to CloudSQL from App Engine

Google example project on GitHub

Upvotes: 0

Related Questions