Connect to Spotify Data from a Connection Pool in Jetty

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

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

    
    <Configure id='spotifydemo' class="org.eclipse.jetty.webapp.WebAppContext">
        <New id="spotifydemo" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg><Ref refid="spotifydemo"/></Arg>
        <Arg>jdbc/spotifydb</Arg>
        <Arg>
          <New class="cdata.jdbc.api.APIDriver">
            <Set name="url">jdbc:api:</Set>
            <Set name="Profile">C:\profiles\Spotify.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="CallbackURL">http://localhost:33333</Set>
          </New>
        </Arg>
      </New>
    </Configure>
    

    Using OAuth Authentication

    Spotify uses OAuth 2.0 for authentication. You will need to create an application in the Spotify Developer Dashboard to obtain your client credentials.

    Setting Up Your Spotify Application

    1. Visit the Spotify Developer Dashboard.
    2. Log in with your Spotify account and click Create app.
    3. Provide an app name, description, and set a Redirect URI (e.g.,
      http://localhost:33333
      for desktop applications).
    4. Copy your Client ID and Client Secret from the app settings.

    Connection Properties

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

    • AuthScheme: Set this to OAuth.
    • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to manage the process to obtain the OAuthAccessToken.
    • OAuthClientId: Set this to your Spotify application's Client ID.
    • OAuthClientSecret: Set this to your Spotify application's Client Secret.
    • Scope: Set this to the required OAuth scopes (space-separated). The default includes all read scopes needed for the tables in this profile.
    • CallbackURL: Set this to the Redirect URI configured in your Spotify application (e.g., http://localhost:33333).

    Example Connection String

    Profile=C:\profiles\Spotify.apip;AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;CallbackURL=http://localhost:33333;
    

    Available OAuth Scopes

    • user-read-private: Read access to user's subscription details and explicit content settings.
    • user-read-email: Read access to user's email address.
    • user-library-read: Read access to a user's saved tracks, albums, episodes, shows, and audiobooks.
    • playlist-read-private: Read access to user's private playlists.
    • playlist-read-collaborative: Read access to collaborative playlists the user follows.
    • user-follow-read: Read access to the list of artists the current user follows.
    • user-read-playback-state: Read access to a user's player state (device, current track, progress).
    • user-read-currently-playing: Read access to a user's currently playing content.
    • user-read-playback-history: Read access to a user's recently played tracks.
    • user-top-read: Read access to a user's top artists and tracks.
  4. Configure the resource in the Web.xml:

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

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

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 Spotify with the API Driver

Connect to Spotify