在GAE上运行的spring hibernate应用程序的问题

时间:2014-08-18 14:45:58

标签: spring hibernate google-app-engine java-ee google-cloud-sql

我有一个使用云SQL在Google云上运行的java(Spring + maven + hibernate)应用程序。每当数据库和应用程序之间存在通信时,它就会抛出这个异常,说无法打开连接通信失败。在最后它给了我:

Caused by: java.net.SocketException: Permission denied: Attempt to access a blocked recipient without permission. (mapped-IPv4)
    at com.google.appengine.api.socket.SocketApiHelper.translateError(SocketApiHelper.java:105)
    at com.google.appengine.api.socket.SocketApiHelper.translateError(SocketApiHelper.java:116)
    at com.google.appengine.api.socket.SocketApiHelper.makeSyncCall(SocketApiHelper.java:82)
    at com.google.appengine.api.socket.AppEngineSocketImpl.createSocket(AppEngineSocketImpl.java:497)
    at com.google.appengine.api.socket.AppEngineSocketImpl.connectToAddress(AppEngineSocketImpl.java:362)
    at com.google.appengine.api.socket.AppEngineSocketImpl.connect(AppEngineSocketImpl.java:352)
    at java.net.Socket.connect(Socket.java:600)
    at java.net.Socket.connect(Socket.java:537)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:250)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
    ... 113 more

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Could not open connection
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:440)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:334)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
    at com.sun.proxy.$Proxy38.loadAdminLoginFromDetails(Unknown Source)
    at com.bullbeardevice.service.impl.CustomAuthenticationManager.authenticate(CustomAuthenticationManager.java:47)

我的hibernate配置文件:

 <!-- Hibernate Session Factory -->
<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:database.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driverClassName}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.abc.model" />
        <property name="hibernateProperties">
            <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.jdbc.batch_size">100</prop>
                <!-- <prop key="hibernate.hbm2ddl.auto">create-drop</prop>  -->
                <prop key="hibernate.current_session_context_class">
                    org.springframework.orm.hibernate4.SpringSessionContext
                </prop>
                <!-- configuration pool via c3p0 -->
                <prop key="hibernate.c3p0.acquire_increment">5</prop>
                <prop key="hibernate.c3p0.idle_test_period">600</prop>
                <prop key="hibernate.c3p0.max_size">25</prop>
                <prop key="hibernate.c3p0.max_statements">25</prop>
                <prop key="hibernate.c3p0.min_size">5</prop>
                <prop key="hibernate.c3p0.timeout">300</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
                <prop key="hibernate.auto_close_session">true</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
  <tx:annotation-driven transaction-manager="transactionManager"/>
  </beans>

我的database.properties:

#db.driverClassName=com.mysql.jdbc.Driver
db.driverClassName=com.google.appengine.api.rdbms.AppEngineDriver
#db.url=jdbc:mysql://192.168.1.3:3306/abc
db.url=jdbc:google:rdbms://xxxx:xxxx/xxxx
#db.url=jdbc:Oracle:thin:@127.0.0.1:1521/XE
db.username=root
db.password=root
#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false
#hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider
#hibernate.cache.provider_class==org.hibernate.cache.EhCacheProvider
#db.sid=XE
# configuration for hibernate search
hibernate.search.default.locking_strategy=simple
#hibernate.search.analyzer=org.apache.lucene.analysis.en.EnglishAnalyzer
hibernate.search.worker.batch_size=100
hibernate.search.indexing_strategy = manual
hibernate.search.default.reader.strategy = shared
hibernate.search.default.worker.thread_pool.size=30
hibernate.search.default.indexwriter.ram_buffer_size=10
hibernate.search.default.indexwriter.merge_max_optimize_size=7
hibernate.search.default.indexwriter.merge_max_size=7

1 个答案:

答案 0 :(得分:0)

部署应用程序时,它将检查访问Cloud SQL的授权(即您访问云sql的授权)。

首次授权时,它会在您的Windows注册表中存储OAUTH访问权限。所以从第二次起,它将直接从注册表中获取访问令牌。

如果此令牌已更改或已过期,您将收到上述错误。因此,您需要删除旧令牌并再次授权。

按照以下步骤从注册表中删除令牌。

打开运行并输入 regedit ,然后点击“打开”。

它将打开注册表编辑器窗口。

然后展开,     HKEY_CURRENT_USER - &gt;软件 - &gt; Javasoft - &gt; Prefs - &gt; com - &gt; Google - &gt;云 - &gt; Sqlservice-&GT;的oauth2

右手边你可以看到Google誓言令牌。 删除除默认值之外的令牌。

现在,如果您部署项目,它将要求您进行授权。它将部署。

希望这会对你有所帮助。