如何为Google Cloud SQL配置Java Hibernate? [英] How to configure Java Hibernate for Google Cloud SQL?

查看:58
本文介绍了如何为Google Cloud SQL配置Java Hibernate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Hibernate + Java + Jersey + MYSql,并且在本地计算机上运行良好.但是,我无法在Google Cloud上建立与MySql的连接.

I am using Hibernate + Java + Jersey + MYSql and it's working fine on local machine. However I am unable to create a connection to MySql on Google Cloud.

我认为MYSql配置和实例创建在Google Cloud上很好,但是我无法通过休眠属性传递所有配置.在此处查看Google的示例代码:

I think MYSql configuration and instance creation is fine on Google Cloud, but I am unable to pass all configuration through hibernate properties. See here what Google's sample code:

// The configuration object specifies behaviors for the connection pool.
HikariConfig config = new HikariConfig();

// Configure which instance and what database user to connect with.
config.setJdbcUrl(String.format("jdbc:mysql:///%s", DB_NAME));
config.setUsername(DB_USER); // e.g. "root", "postgres"
config.setPassword(DB_PASS); // e.g. "my-password"

// For Java users, the Cloud SQL JDBC Socket Factory can provide authenticated connections.
// See https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory for details.
config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.mysql.SocketFactory");
config.addDataSourceProperty("cloudSqlInstance", CLOUD_SQL_CONNECTION_NAME);
config.addDataSourceProperty("useSSL", "false");

// ... Specify additional connection properties here.
// ...

// Initialize the connection pool using the configuration object.
DataSource pool = new HikariDataSource(config);

现在我不知道如何在休眠中传递cloudSqlInstancesocketFactory.在这里,我尝试传递这些参数,但是它不起作用:

Now I dont know how to pass cloudSqlInstance and socketFactory in Hibernate. Here what I tried to pass these parameters but it's not working:

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://google/pashumandi_db?cloudSqlInstance=pashuserver:asia-south1:mysql-instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory
hibernate.connection.username = abc_user
hibernate.connection.password = 12345678
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

能否让我知道与Google Cloud上的MySql连接的正确的休眠配置?谢谢.

Could you please let me know what is correct hibernate configuration to connect with MySql on Google Cloud? Thanks.

推荐答案

在Google Cloud Console的实例详细信息"页面上找到实例的INSTANCE_CONNECTION_NAME.它使用PROJECT_ID:REGION:INSTANCE_ID格式,用于标识您要连接的Cloud SQL实例.

Find the INSTANCE_CONNECTION_NAME for the instance on the Instance details page on Google Cloud Console. It uses the format PROJECT_ID:REGION:INSTANCE_ID, and is used to identify the Cloud SQL instance you are connecting to.

要启用本地TCP端口,请将以下内容添加到项目的app.yaml文件中:

To enable a local TCP port, add the following to your project's app.yaml file:

runtime: java
env: flex
beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<PORT>

然后这是我的hibernate.properties文件:

Then here is my hibernate.properties file:

hibernate.connection.driver_class = com.mysql.jdbc.Driver
hibernate.connection.url = jdbc:mysql://172.17.0.1:PORT_NUMBER_IN_YAML_FILE
hibernate.connection.username = DB_USERNAME
hibernate.connection.password = DB_PASSWORD
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
hibernate.default_schema = DATABASE_NAME
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create

有关更多详细信息,您可以访问: https://cloud. google.com/sql/docs/mysql/connect-app-engine

For more details you can visit: https://cloud.google.com/sql/docs/mysql/connect-app-engine

这篇关于如何为Google Cloud SQL配置Java Hibernate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆