SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

Glenn Paulley氏 データベース関連ブログ 翻訳記事(AD)

SQL Anywhere 12.0.1でのNHibernate 3.2.0 GAの使用

原文:Using NHibernate 3.2.0 GA with SQL Anywhere 12.0.1

  • このエントリーをはてなブックマークに追加

SybaseSQLAnywhere12Driver.cs

 思い出していただきたいのですが、自分のNHibernateアプリケーションのApp.configファイルには、データベースへの接続に使用するADO.NETプロバイダと自分のNHibernateアプリケーションとのリンケージを提供するNHibernateドライバの名前を指定したはずです。NHibernate 3.2.0ディストリビューションに含まれるSQL Anywhere用のドライバは、SybaseSQLAnywhereDriver.csです(余談ですが、NHibernate 3.2.0ディストリビューションに含まれるもう1つのSQL AnywhereドライバであるSybaseAsaClientDriver.csは、旧式のためもう使用すべきでないと考えてよいでしょう)。

 SQL Anywhere 12.0.1には、SQL Anywhere ADO.NETプロバイダの''2とおりの異なる実装''が用意されています。1つは.NET V4.0のサポートを提供するiAnywhere.Data.SQLAnywhere.v4.0.dllで、もう1つは.NET V3.5のサポートを提供するiAnywhere.Data.SQLAnywhere.v3.5.dllです。このバージョンの違いにより、SQL Anywhereドライバに文字列リテラルとして埋め込まれた「iAnywhere.Data.SQLAnywhere.dll」というDLL名が不適切になりました。そのため、ドライバを変更するか、また別の対策として、以前のSQL Anywhereリリースに含まれていた旧式のADO.NETプロバイダを使用するという方法をとらないと、アプリケーションはADO.NETライブラリを探し出す処理に失敗し、SQL Anywhere 12.0.1サーバへの接続の試みが失敗して、例外を生成します。

 そこで、私はSybaseSQLAnywhere12Driver.csという新しいドライバクラスを作成しました。次に示すとおり、このクラスにはSQL Anywhere Version 4.0 ADO.NETプロバイダの正しい名前が含まれています。

using System;
 
namespace NHibernate.Driver
{
    /// <summary>
    /// SQL Dialect for SQL Anywhere 12 - for the NHibernate 3.2.0 distribution
    /// Copyright (C) 2011 Glenn Paulley
        /// Contact: http://iablog.sybase.com/paulley
    ///
    /// This NHibernate dialect for SQL Anywhere 12 is a contribution to the NHibernate
        /// open-source project. It is intended to be included in the NHibernate 
        /// distribution and is licensed under LGPL.
    ///
    /// This library is free software; you can redistribute it and/or
    /// modify it under the terms of the GNU Lesser General Public
    /// License as published by the Free Software Foundation; either
    /// version 2.1 of the License, or (at your option) any later version.
    ///
    /// This library is distributed in the hope that it will be useful,
    /// but WITHOUT ANY WARRANTY; without even the implied warranty of
    /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    /// Lesser General Public License for more details.
    ///
    /// You should have received a copy of the GNU Lesser General Public
    /// License along with this library; if not, write to the Free Software
    /// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    ///
    /// </summary>
    ///
    /// <remarks>
    /// The SybaseSQLAnywhere12Driver provides a database driver for Sybase SQL Anywhere 12
        /// using the versioned ADO.NET driver iAnywhere.Data.SQLAnywhere.v4.0.
    /// </remarks>
    public class SybaseSQLAnywhere12Driver : ReflectionBasedDriver
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SybaseSQLAnywhereDriver"/> class.
        /// </summary>
        /// <exception cref="HibernateException">
        /// Thrown when the iAnywhere.Data.SQLAnywhere.v4.0 assembly is not and can not be loaded.
        /// </exception>
        public SybaseSQLAnywhere12Driver()
            : base("iAnywhere.Data.SQLAnywhere", "iAnywhere.Data.SQLAnywhere.v4.0", "iAnywhere.Data.SQLAnywhere.SAConnection", "iAnywhere.Data.SQLAnywhere.SACommand")
        {
        }
 
        public override bool UseNamedPrefixInSql
        {
            get { return false; }
        }
 
        public override bool UseNamedPrefixInParameter
        {
            get { return false; }
        }
 
        public override string NamedPrefix
        {
            get { return String.Empty; }
        }
    }
}

 残念なことですが、NHibernateには、ADO.NETライブラリやその設定に変更があってもNHibernate DLLを再コンパイルせずに対応できるようにする一連の構成パラメータはないようです。私は、この点を今後の改善課題としてJulianに提案するつもりです。それまでの間は、この新しいSybaseSQLAnywhere12Driver.csファイルをNHibernate/src/NHibernate/Driverディレクトリに置き、NHibernate DLLの再コンパイルをすることが必要です。再コンパイルしたDLLを使用するには、アプリケーションのApp.configファイルを変更しなければなりません。次に、私がHelloNHibernateアプリケーションで使用したApp.configファイルを紹介します。このファイルも前述の.zipアーカイブに含まれています。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" 
             type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">
        NHibernate.Connection.DriverConnectionProvider
      </property>
      <property name="connection.driver_class">
        NHibernate.Driver.SybaseSQLAnywhere12Driver
      </property>
      <property name="connection.connection_string">
        uid=dba;pwd=sql
      </property>
      <property name="show_sql">true</property>
      <property name="dialect">NHibernate.Dialect.SybaseSQLAnywhere12Dialect</property>
    </session-factory>
  </hibernate-configuration>
</configuration>

 いつものように、NHibernateとそのSQL Anywhereサポートについて質問がある場合は、私まで直接お寄せください。

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
Glenn Paulley氏 データベース関連ブログ 翻訳記事連載記事一覧

もっと読む

この記事の著者

Glenn Paulley(Glenn Paulley)

カナダ オンタリオ州 ウォータールー R&DセンターにてSQL Anywhere 開発における Director of Engineering としてクエリ・オプティマイザなどの開発をリードしている。・IvanAnywhere

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

【AD】本記事の内容は記事掲載開始時点のものです 企画・制作 株式会社翔泳社

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/6159 2011/12/01 14:00

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング