将 GCP Cloud SQL SocketFactory 与 Hibernate Configuration 结合使用



我正在尝试从GCP App Engine (GAE( Standard连接到Cloud SQL Postgres数据库实例。 将数据库实例连接名称(my-project-id:europe-west1:db-instance-name(与带休眠的 Cloud SQL SocketFactory 结合使用。

    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;

无法创建请求的服务 [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]

我认为您使用的实现不适合App Engine Standard,您应该检查官方文档中可以找到的Google实现。

从App Engine连接到CloudSQL的官方Google文档

GitHub 上的谷歌示例项目

相关内容

  • 没有找到相关文章

最新更新