Connect to Outlook Data from a Connection Pool in Jetty

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
The Outlook JDBC Driver supports connection pooling: This article shows how to connect faster to Outlook data from Web apps in Jetty.

The CData JDBC driver for Outlook is easy to integrate with Java Web applications. This article shows how to efficiently connect to Outlook data in Jetty by configuring the driver for connection pooling. You will configure a JNDI resource for Outlook 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 Outlook data source at the level of the Web app, in WEB-INF\jetty-env.xml.

    
    <Configure id='outlookdemo' class="org.eclipse.jetty.webapp.WebAppContext">
        <New id="outlookdemo" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg><Ref refid="outlookdemo"/></Arg>
        <Arg>jdbc/outlookdb</Arg>
        <Arg>
          <New class="cdata.jdbc.api.APIDriver">
            <Set name="url">jdbc:api:</Set>
            <Set name="Profile">C:\profiles\Outlook.apip</Set>
            <Set name="AuthScheme">OAuth</Set>
            <Set name="InitiateOAuth">GETANDREFRESH</Set>
            <Set name="OAuthClientId">your_client_id</Set>
            <Set name="OAuthClientSecret">your_client_secret</Set>
            <Set name="TenantId">your_tenant_id</Set>
            <Set name="CallbackUrl">http://localhost:33333</Set>
          </New>
        </Arg>
      </New>
    </Configure>
    

    Using OAuth Authentication

    Microsoft Graph API uses OAuth 2.0 for authentication. You must register an application in the Microsoft Azure Portal to obtain OAuth credentials (Client ID and Client Secret).

    Obtaining OAuth Credentials

    1. Log in to the Azure Portal.
    2. Navigate to Azure Active Directory > App registrations.
    3. Click New registration to create a new application.
    4. Enter an application name and select the appropriate account types.
    5. Set the Redirect URI to your application's callback URL (e.g., http://localhost:33333 for desktop apps).
    6. Click Register to create the application.
    7. On the application overview page, copy the Application (client) ID - this is your OAuthClientId.
    8. Navigate to Certificates & secrets and create a new client secret.
    9. Copy the client secret value - this is your OAuthClientSecret.
    10. Navigate to API permissions and add the required Microsoft Graph API permissions:
      • Mail.Read - For accessing email messages
      • Contacts.Read - For accessing contacts
      • Calendars.Read - For accessing calendar events
      • Tasks.Read - For accessing To Do tasks
      • offline_access - For obtaining refresh tokens
    11. Click Grant admin consent to grant these permissions.

    Connecting with OAuth

    After setting the following connection properties, you are ready to connect:

    • AuthScheme: Set this to OAuth.
    • InitiateOAuth: Set this to GETANDREFRESH. The CData API Profile for Outlook will automatically walk through the OAuth process in order to obtain the access token.
    • OAuthClientId: Set this to the Application (client) ID from Azure Portal.
    • OAuthClientSecret: Set this to the client secret value from Azure Portal.
    • TenantId: Set this to your Azure AD tenant identifier (GUID or domain name like 'contoso.onmicrosoft.com').
    • CallbackURL: Set this to the Redirect URI you specified in your app registration (e.g., http://localhost:33333 for desktop apps).

    Example connection string

    Profile=C:\profiles\Outlook.apip;AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;TenantId=your_tenant_id;CallbackUrl=http://localhost:33333;
    
  4. Configure the resource in the Web.xml:

    
      jdbc/outlookdb
      javax.sql.DataSource
      Container
    
    
  5. You can then access Outlook with a lookup to java:comp/env/jdbc/outlookdb:

    InitialContext ctx = new InitialContext();
    DataSource myoutlook = (DataSource)ctx.lookup("java:comp/env/jdbc/outlookdb");
    

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.

Ready to get started?

Connect to live data from Outlook with the API Driver

Connect to Outlook