Connecting to PostgreSQL in the java application engine

存储架构 2018-01-14 阅读原文

I'm trying to connect to PostgreSQL server from java app engine. On my local machine code works perfectly, but when I deploy code to appengine, I get this exception:

Uncaught exception from servlet
    at java.util.Hashtable.put(
    at java.util.Properties.setProperty(
    at org.postgresql.Driver.loadDefaultProperties(
    at org.postgresql.Driver.access$000(
    at org.postgresql.Driver$
    at org.postgresql.Driver.getDefaultProperties(
    at org.postgresql.Driver.connect(
    at java.sql.DriverManager.getConnection(
    at java.sql.DriverManager.getConnection(
    at guestbook.PostgreServlet.doGet(
    at javax.servlet.http.HttpServlet.service(
    at javax.servlet.http.HttpServlet.service(
    at org.mortbay.jetty.servlet.ServletHolder.handle(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(
    at org.mortbay.jetty.servlet.ServletHandler.handle(
    at org.mortbay.jetty.servlet.SessionHandler.handle(
    at org.mortbay.jetty.handler.ContextHandler.handle(
    at org.mortbay.jetty.webapp.WebAppContext.handle(
    at org.mortbay.jetty.handler.HandlerWrapper.handle(
    at org.mortbay.jetty.Server.handle(
    at org.mortbay.jetty.HttpConnection.handleRequest(
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(
    at org.mortbay.jetty.HttpConnection.handle(

I am using postgresql-9.2-1003.jdbc4.jar and I have this jdbc as library and also inside my "war/WEB-INF/lib" folder. My java code is here:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PostgreServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();

        Connection conn = null;
        String result = "";
        try {
            conn = DriverManager.getConnection("jdbc:postgresql://", "xxx", "xxxx");
            if(conn == null){
                pw.println("Connection null");
            ResultSet rs = conn.createStatement().executeQuery(
                    "SELECT * FROM pg_catalog.pg_tables");
            while ( {
                result += rs.getString("tablename") + "
"; } pw.println(result); } catch (SQLException e) { e.printStackTrace(); } finally { if(conn != null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }

Google don't allow jdbc out, only http out.

Look at Google Secure Data Connector (which is deprecated, but works until 2015).

Connections out from the Google App Engine are only HTTP based!

Google Cloud SQL

There is some functionality in Google Cloud SQL that you might wanna look at:


There are solutions to reach databases over http (e.g.

The URLFetch API

App Engine applications can communicate with other applications or access other resources on the web by fetching URLs. An app can use the URL Fetch service to issue HTTP and HTTPS requests and receive responses. The URL Fetch service uses Google's network infrastructure for efficiency and scaling purposes.

Socket API

App Engine supports regular outbound Java sockets such as and There is currently no support for sockets via java.nio.SocketChannel or other java.nio classes.
Sockets are only available for paid apps!

(another way to use outbounds, mentioned by Peter Knego)

But I would not recommend (accessing external databases easily over internet, with public IPs) , this solution are no-no for me, it is a high security issue - my opinion!
Hello, buddy!

责编内容by:Hello, buddy!阅读原文】。感谢您的支持!


What happens behind the scenes for finalize() meth... One Java question typically asked at interviews is “What is the purpose of finalize() method?” While you can respon...
Java内存区域 线程私有。 一块较小的内存区域,当前线程所执行的字节码的行号指示器。 如果执行的是一个Java方法,计数器记录的是正在执行的虚拟机字节码指令的地址;如果正在执行的是一个Native方法,计数器值为空。 此内存区域,是唯一一...
Java日志-SLF4J使用与源码分析 SLF4J全称The Simple Logging Facade for Java,Java简易日志门面,将接口抽象与实现隔离开,在不修改代码的情况下使用不同的日志实现。 SLF4J支持的日志实现有: log4j lo...
自己动手写把”锁”—LockSupport深入浅出... 本篇是《自己动手写把"锁"》系列技术铺垫的最后一个知识点。本篇主要讲解LockSupport工具类,它用来实现线程的挂起和唤醒。 LockSupport是Java6引入的一个工具类,它简单灵活,应用广泛。 一、简单 俗话说,没有...
eclipse 快捷键及使用技巧 一、程序的编译和运行的环境配置(一般不改)window -- Preferences -- Java 编译环境:Compiler 默认选中的就是最高版本。 运行环境:Installed JREs 默认会找你...