Connect to Google Cloud Storage Data from a Connection Pool in Jetty

Ready to get started?

Download for a free trial:

Download Now

Learn more:

Google Cloud Storage JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Google Cloud Storage.



The Google Cloud Storage JDBC Driver supports connection pooling: This article shows how to connect faster to Google Cloud Storage data from Web apps in Jetty.

The CData JDBC driver for Google Cloud Storage is easy to integrate with Java Web applications. This article shows how to efficiently connect to Google Cloud Storage data in Jetty by configuring the driver for connection pooling. You will configure a JNDI resource for Google Cloud Storage in Jetty.

Configure the JDBC Driver for Salesforce as a JNDI Data Source

Follow the steps below to connect to Salesforce from Jetty.

  1. Enable the JNDI module for your Jetty base. The following command enables JNDI from the command-line:

    java -jar ../start.jar --add-to-startd=jndi
  2. Add the CData and license file, located in the lib subfolder of the installation directory, into the lib subfolder of the context path.
  3. Declare the resource and its scope. Enter the required connection properties in the resource declaration. This example declares the Google Cloud Storage data source at the level of the Web app, in WEB-INF\jetty-env.xml.

    <Configure id='googlecloudstoragedemo' class="org.eclipse.jetty.webapp.WebAppContext"> <New id="googlecloudstoragedemo" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg><Ref refid="googlecloudstoragedemo"/></Arg> <Arg>jdbc/googlecloudstoragedb</Arg> <Arg> <New class="cdata.jdbc.googlecloudstorage.GoogleCloudStorageDriver"> <Set name="url">jdbc:googlecloudstorage:</Set> <Set name="ProjectId">'project1'</Set> <Set name="InitiateOAuth">GETANDREFRESH</Set> </New> </Arg> </New> </Configure>

    Authenticate with a User Account

    You can connect without setting any connection properties for your user credentials. After setting InitiateOAuth to GETANDREFRESH, you are ready to connect.

    When you connect, the Google Cloud Storage OAuth endpoint opens in your default browser. Log in and grant permissions, then the OAuth process completes

    Authenticate with a Service Account

    Service accounts have silent authentication, without user authentication in the browser. You can also use a service account to delegate enterprise-wide access scopes.

    You need to create an OAuth application in this flow. See the Help documentation for more information. After setting the following connection properties, you are ready to connect:

    • InitiateOAuth: Set this to GETANDREFRESH.
    • OAuthJWTCertType: Set this to "PFXFILE".
    • OAuthJWTCert: Set this to the path to the .p12 file you generated.
    • OAuthJWTCertPassword: Set this to the password of the .p12 file.
    • OAuthJWTCertSubject: Set this to "*" to pick the first certificate in the certificate store.
    • OAuthJWTIssuer: In the service accounts section, click Manage Service Accounts and set this field to the email address displayed in the service account Id field.
    • OAuthJWTSubject: Set this to your enterprise Id if your subject type is set to "enterprise" or your app user Id if your subject type is set to "user".
    • ProjectId: Set this to the Id of the project you want to connect to.

    The OAuth flow for a service account then completes.

  4. Configure the resource in the Web.xml:

    jdbc/googlecloudstoragedb javax.sql.DataSource Container
  5. You can then access Google Cloud Storage with a lookup to java:comp/env/jdbc/googlecloudstoragedb: InitialContext ctx = new InitialContext(); DataSource mygooglecloudstorage = (DataSource)ctx.lookup("java:comp/env/jdbc/googlecloudstoragedb");

More Jetty Integration

The steps above show how to configure the driver in a simple connection pooling scenario. For more use cases and information, see the Working with Jetty JNDI chapter in the Jetty documentation.