【发布时间】:2014-08-07 13:44:54
【问题描述】:
我开发了谷歌端点 API 并使用 JDBC 访问云 sql 数据库。现在我想使用 ORM 工具来访问云 sql 数据库。 那么谷歌应用引擎支持哪些ORM工具而不是JDO和JPA? 我的意思是,hibernate 支持 GAE 吗?
【问题讨论】:
-
你能在GAE中集成hibernate吗?
标签: java hibernate google-app-engine orm
我开发了谷歌端点 API 并使用 JDBC 访问云 sql 数据库。现在我想使用 ORM 工具来访问云 sql 数据库。 那么谷歌应用引擎支持哪些ORM工具而不是JDO和JPA? 我的意思是,hibernate 支持 GAE 吗?
【问题讨论】:
标签: java hibernate google-app-engine orm
【讨论】:
EntityManagerFactory:github.com/GoogleCloudPlatform/…
是的,Google App Engine 支持 Hibernate ORM 与 Google Cloud SQL 的结合。 我们在几乎所有的 Google App Engine 项目中都使用这种组合,有时甚至使用连接池;
Can i use HikariCP on Google App Engine
Cloud SQL 甚至可以在开发过程中使用;
Using Google Cloud SQL during Java appengine development (instead of local MySQL instance)
对于 Hibernate 连接,我们实现了自己的 ConnectionProvider(我从 appengine-java-connection-pool 得到这个想法)。在persistence.xml中
使用 BasicConnectionProvider;
public class BasicConnectionProvider implements ConnectionProvider, Configurable, Stoppable
{
private static final long serialVersionUID = 5535849343546193022L;
private static final org.jboss.logging.Logger LOGGER = org.jboss.logging.Logger.getLogger(com.project.server.hibernate.BasicConnectionProvider.class);
private DriverManagerDataSource mDriverManagerDataSource = null;
private int mConnectionCount;
// *************************************************************************
// Configurable
// *************************************************************************
@SuppressWarnings("rawtypes")
@Override
public void configure(Map pProperties) throws HibernateException
{
try
{
LOGGER.debug("Configuring DataSource");
DriverManagerDataSource lDriverManagerDataSource = new DriverManagerDataSource();
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production)
{
lDriverManagerDataSource.setDriverClassName("com.mysql.jdbc.GoogleDriver");
lDriverManagerDataSource.setUrl("jdbc:google:mysql://appengine-id:europe-west1:project/project");
}
else
{
lDriverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");
lDriverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/project");
}
lDriverManagerDataSource.setUsername("root");
lDriverManagerDataSource.setPassword("password");
Properties lProperties = loadConfiguration(pProperties);
lProperties.put("useSSL", "false");
lProperties.put("cachePrepStmts", "true");
lProperties.put("useServerPrepStmts", "true");
lProperties.put("prepStmtCacheSize", "500");
lProperties.put("prepStmtCacheSqlLimit", "2048");
lDriverManagerDataSource.setConnectionProperties(lProperties);
setDriverManagerDataSource(lDriverManagerDataSource);
mConnectionCount = 0;
}
catch (Exception e)
{
throw new HibernateException(e);
}
LOGGER.debug("DataSource Configured");
}
public static Properties loadConfiguration(Map props)
{
Properties lProperties = new Properties();
copyProperty(AvailableSettings.ISOLATION, props, "transactionIsolation", lProperties);
copyProperty(AvailableSettings.AUTOCOMMIT, props, "autoCommit", lProperties);
copyProperty(AvailableSettings.DRIVER, props, "driverClassName", lProperties);
copyProperty(AvailableSettings.URL, props, "jdbcUrl", lProperties);
copyProperty(AvailableSettings.USER, props, "username", lProperties);
copyProperty(AvailableSettings.PASS, props, "password", lProperties);
return lProperties;
}
@SuppressWarnings("rawtypes")
private static void copyProperty(String srcKey, Map src, String dstKey, Properties dst)
{
if (src.containsKey(srcKey))
{
dst.setProperty(dstKey, (String) src.get(srcKey));
}
}
// *************************************************************************
// ConnectionProvider
// *************************************************************************
@Override
public Connection getConnection() throws SQLException
{
Connection lConnection = null;
if (mDriverManagerDataSource != null)
{
lConnection = mDriverManagerDataSource.getConnection();
}
mConnectionCount++;
return lConnection;
}
@Override
public void closeConnection(Connection pConnection) throws SQLException
{
mConnectionCount--;
pConnection.close();
}
@Override
public boolean supportsAggressiveRelease()
{
return false;
}
@Override
@SuppressWarnings("rawtypes")
public boolean isUnwrappableAs(Class unwrapType)
{
return ConnectionProvider.class.equals(unwrapType)
|| com.project.server.hibernate.BasicConnectionProvider.class.isAssignableFrom(unwrapType)
|| DataSource.class.isAssignableFrom(unwrapType);
}
@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> unwrapType)
{
if (ConnectionProvider.class.equals(unwrapType) ||
com.project.server.hibernate.BasicConnectionProvider.class.isAssignableFrom(unwrapType))
{
return (T) this;
}
else if (DataSource.class.isAssignableFrom(unwrapType))
{
return (T) mDriverManagerDataSource;
}
else
{
throw new UnknownUnwrapTypeException(unwrapType);
}
}
// *************************************************************************
// Stoppable
// *************************************************************************
@Override
public void stop()
{
}
public DriverManagerDataSource getDriverManagerDataSource()
{
return mDriverManagerDataSource;
}
public void setDriverManagerDataSource(DriverManagerDataSource pDriverManagerDataSource)
{
mDriverManagerDataSource = pDriverManagerDataSource;
}
}
【讨论】: