Jetty の接続プールから Cloudant のデータ に接続
CData JDBC Driver for Cloudant は、Java Web アプリケーションへの統合が簡単です。この記事では、接続プールを設定することで Jetty から Cloudant のデータ に効率的に接続する方法を説明します。Jetty で Cloudant 用の JNDI リソースを設定していきましょう。
Salesforce 用の JDBC Driver を JNDI データソースとして設定
以下のステップで Jetty から Salesforce に接続します。
Jetty ベースで JNDI モジュールを有効にします。次のコマンドは、コマンドラインから JNDI を有効にします。
java -jar ../start.jar --add-to-startd=jndi
- インストールディレクトリの lib サブフォルダにある CData およびライセンスファイルを、コンテキストパスの lib サブフォルダに追加します。
-
リソースとそのスコープを宣言します。リソース宣言に必要な接続プロパティを入力します。次の例では、WEB-INF\jetty-env.xml に Web アプリレベルで Cloudant データソースを宣言しています。
<Configure id='cloudantdemo' class="org.eclipse.jetty.webapp.WebAppContext"> <New id="cloudantdemo" class="org.eclipse.jetty.plus.jndi.Resource"> <Arg><Ref refid="cloudantdemo"/></Arg> <Arg>jdbc/cloudantdb</Arg> <Arg> <New class="cdata.jdbc.cloudant.CloudantDriver"> <Set name="url">jdbc:cloudant:</Set> <Set name="User">abc123</Set> <Set name="Password">abcdef</Set> </New> </Arg> </New> </Configure>Cloudant 接続プロパティの取得・設定方法
Cloudant への接続には、以下の接続プロパティを設定します。
- User:ユーザー名に設定。
- Password:パスワードに設定。
-
Web.xml でリソースを設定します。
jdbc/cloudantdb javax.sql.DataSource Container
-
これで、java:comp/env/jdbc/cloudantdb をルックアップして Cloudant にアクセスできます。
InitialContext ctx = new InitialContext(); DataSource mycloudant = (DataSource)ctx.lookup("java:comp/env/jdbc/cloudantdb");
Jetty 統合の詳細
上記のステップでは、シンプルな接続プールのシナリオでドライバーを設定する方法を説明しました。その他のユースケースや詳細については、Jetty ドキュメントのWorking with Jetty JNDI の章を参照してください。