NHibernate需要更长的时间来执行简单的查询

时间:2012-08-17 01:23:50

标签: nhibernate orm nhibernate-mapping queryover nhibernate-criteria

我面临着非常奇怪的问题,NHibernate现在变得很头疼了。

NHibernate执行这样一个简单的查询需要比预期时间(几毫秒)更长的时间(2-3分钟)。数据库是Oracle,我正在使用ODP驱动程序。我已经检查了NHibernate和Spring所有必要的配置,这对我来说还不错。当我在sqldeveloper中执行相同的查询时,它以毫秒为单位给出结果。

FYI - 当我执行另一个具有三个内连接的查询时,复杂模型具有相同的NHibernate配置,我得到了预期的结果。

在调试日志中,我可以看到下面的行浪费时间:

2012-08-17 09:53:20,754 [TestRunnerThread] DEBUG - NHibernate.Connection.DriverConnectionProvider - Obtaining IDbConnection from Driver
2012-08-17 09:55:09,369 [TestRunnerThread] DEBUG - NHibernate.AdoNet.AbstractBatcher - ExecuteReader took 108720 ms

NHibernate属性设置:

<nhibernatePropertiesSettings>
<setting name="nhibernate.connection.provider" serializeAs="String">
 <value>NHibernate.Connection.DriverConnectionProvider</value>
</setting>
<setting name="nhibernate.connection.driver.class" serializeAs="String">
 <value>NHibernate.Driver.OracleDataClientDriver</value>
</setting>
<setting name="nhibernate.dialect" serializeAs="String">
 <value>NHibernate.Dialect.Oracle10gDialect</value>
</setting>
<setting name="nhibernate.show.sql" serializeAs="String">
 <value>true</value>
</setting>
<setting name="nhibernate.query.substitutions" serializeAs="String">
 <value>true 1, false 0, yes 'Y', no 'N'</value>
</setting>
<setting name="nhibernate.use.proxy.validator" serializeAs="String">
 <value>false</value>
</setting>
<setting name="nhibernate.template.flush.mode" serializeAs="String">
 <value>Never</value>
</setting>
</nhibernatePropertiesSettings>

Spring属性设置:

<springPropertiesSettings>
 <setting name="spring.db.provider" serializeAs="String">
  <value>OracleODP-11-2.0</value>
 </setting>
</springPropertiesSettings>

映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="XYZ.PaymentInvestigation.Service.Model.PaymentMessageHistory, PaymentInvestigation.Service.Model" table="MESSAGE_HIST_T" lazy="false">    
    <composite-id name="PaymentMessageHistoryId" class="XYZ.PaymentInvestigation.Service.Model.PaymentMessageHistoryId, PaymentInvestigation.Service.Model" unsaved-value="undefined">
      <key-property name="TransactionDate" column="TRN_DATE" />
      <key-property name="TransactionReferenceNumber" column="TRN_NUMBER" />
      <key-property name="TransactionTimeStamp" column="TRN_TIMESTAMP" />
      <key-property name="HistoryNumber" type="AnsiString" column="HIST_NO" />
      <key-property name="SubHistoryNumber" type="AnsiString" column="SUB_HIST_NO" />
    </composite-id>
    <property name="EntryType" column="ENTRY_TYPE" update="false" insert="false" />
    <property name="Location" column="LOC" update="false" insert="false" />
    <property name="QueLineId" column="QUE_LINE_ID" update="false" insert="false" />
    <property name="DateTime" column="DATE_TIME" update="false" insert="false" />
    <property name="SequenceNo" column="SEQUENCE_NO" update="false" insert="false" />
    <property name="OperatorInitials" column="OPR_INITIALS" update="false" insert="false" />
    <property name="Amount" column="AMOUNT" update="false" insert="false" />
    <property name="MsgInfo" column="MSG_INFO" update="false" insert="false" />
    <property name="RecordExpired" column="RECORD_EXPIRED" update="false" insert="false" />
    <property name="RecordUpdated" column="RECORD_UPDATED" update="false" insert="false" />
    <property name="Details" column="DETAILS" update="false" insert="false" /> 
 </class>

查询:

IList<PaymentMessageHistory> paymentMessageHistories =
    HibernateTemplate.ExecuteFind(session => session
        .QueryOver<PaymentMessageHistory>()
        .Where(x =>
            x.PaymentMessageHistoryId.TransactionDate == paymentMessageId.TransactionDate &&
            x.PaymentMessageHistoryId.TransactionReferenceNumber == paymentMessageId.TransactionReferenceNumber)
        .List());

PaymentMessageHistoryId Model:

public class PaymentMessageHistoryId : IEquatable<PaymentMessageHistoryId>
{
    public virtual DateTime TransactionDate { get; set; }
    public virtual int TransactionReferenceNumber { get; set; }
    public virtual double TransactionTimeStamp { get; set; }
    public virtual string HistoryNumber { get; set; }
    public virtual string SubHistoryNumber { get; set; }

    public bool Equals(PaymentMessageHistoryId other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return other.TransactionDate.Equals(TransactionDate) && other.TransactionReferenceNumber == TransactionReferenceNumber && other.TransactionTimeStamp.Equals(TransactionTimeStamp) && Equals(other.HistoryNumber, HistoryNumber) && Equals(other.SubHistoryNumber, SubHistoryNumber);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof (PaymentMessageHistoryId)) return false;
        return Equals((PaymentMessageHistoryId) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = TransactionDate.GetHashCode();
            result = (result*397) ^ TransactionReferenceNumber;
            result = (result*397) ^ TransactionTimeStamp.GetHashCode();
            result = (result*397) ^ (HistoryNumber != null ? HistoryNumber.GetHashCode() : 0);
            result = (result*397) ^ (SubHistoryNumber != null ? SubHistoryNumber.GetHashCode() : 0);
            return result;
        }
    }

    public static bool operator ==(PaymentMessageHistoryId left, PaymentMessageHistoryId right)
    {
        return Equals(left, right);
    }

    public static bool operator !=(PaymentMessageHistoryId left, PaymentMessageHistoryId right)
    {
        return !Equals(left, right);
    }

    public override string ToString()
    {
        return string.Format("TransactionDate: {0}, TransactionReferenceNumber: {1}, TransactionTimeStamp: {2}, HistoryNumber: {3}, SubHistoryNumber: {4}", TransactionDate, TransactionReferenceNumber, TransactionTimeStamp, HistoryNumber, SubHistoryNumber);
    }

PaymentMessageHistory模型:

public class PaymentMessageHistory : IEquatable<PaymentMessageHistory>
{
    public virtual PaymentMessageHistoryId PaymentMessageHistoryId { get; set; }
    public virtual string EntryType { get; set; }
    public virtual string Location { get; set; }
    public virtual string QueLineId { get; set; }
    public virtual string DateTime { get; set; }
    public virtual string SequenceNo { get; set; }
    public virtual string OperatorInitials { get; set; }
    public virtual decimal Amount { get; set; }
    public virtual string MsgInfo { get; set; }
    public virtual double RecordExpired { get; set; }
    public virtual string RecordUpdated { get; set; }
    public virtual string Details { get; set; }

    public bool Equals(PaymentMessageHistory other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(other.PaymentMessageHistoryId, PaymentMessageHistoryId) && Equals(other.EntryType, EntryType) && Equals(other.Location, Location) && Equals(other.QueLineId, QueLineId) && Equals(other.DateTime, DateTime) && Equals(other.SequenceNo, SequenceNo) && Equals(other.OperatorInitials, OperatorInitials) && other.Amount == Amount && Equals(other.MsgInfo, MsgInfo) && other.RecordExpired.Equals(RecordExpired) && Equals(other.RecordUpdated, RecordUpdated) && Equals(other.Details, Details);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != typeof (PaymentMessageHistory)) return false;
        return Equals((PaymentMessageHistory) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            int result = (PaymentMessageHistoryId != null ? PaymentMessageHistoryId.GetHashCode() : 0);
            result = (result*397) ^ (EntryType != null ? EntryType.GetHashCode() : 0);
            result = (result*397) ^ (Location != null ? Location.GetHashCode() : 0);
            result = (result*397) ^ (QueLineId != null ? QueLineId.GetHashCode() : 0);
            result = (result*397) ^ (DateTime != null ? DateTime.GetHashCode() : 0);
            result = (result*397) ^ (SequenceNo != null ? SequenceNo.GetHashCode() : 0);
            result = (result*397) ^ (OperatorInitials != null ? OperatorInitials.GetHashCode() : 0);
            result = (result*397) ^ Amount.GetHashCode();
            result = (result*397) ^ (MsgInfo != null ? MsgInfo.GetHashCode() : 0);
            result = (result*397) ^ RecordExpired.GetHashCode();
            result = (result*397) ^ (RecordUpdated != null ? RecordUpdated.GetHashCode() : 0);
            result = (result*397) ^ (Details != null ? Details.GetHashCode() : 0);
            return result;
        }
    }

    public static bool operator ==(PaymentMessageHistory left, PaymentMessageHistory right)
    {
        return Equals(left, right);
    }

    public static bool operator !=(PaymentMessageHistory left, PaymentMessageHistory right)
    {
        return !Equals(left, right);
    }

    public override string ToString()
    {
        return string.Format("PaymentMessageHistoryId: {0}, EntryType: {1}, Location: {2}, QueLineId: {3}, DateTime: {4}, SequenceNo: {5}, OperatorInitials: {6}, Amount: {7}, MsgInfo: {8}, RecordExpired: {9}, RecordUpdated: {10}, Details: {11}", PaymentMessageHistoryId, EntryType, Location, QueLineId, DateTime, SequenceNo, OperatorInitials, Amount, MsgInfo, RecordExpired, RecordUpdated, Details);
    }

我完成了这个NHibernate taking a long time to run query,但没有为我工作。

请帮忙!

此致 Milind

2 个答案:

答案 0 :(得分:0)

public class OracleDataClientDriver2 : OracleDataClientDriver
{
    protected override void InitializeParameter(IDbDataParameter dbParam, string name, SqlType sqlType)
    {
        switch (sqlType.DbType)
        {
            //Timestamp columns not indexed by Oracle 11g date columns. - Use Date 
            case DbType.DateTime:
                base.InitializeParameter(dbParam, name, SqlTypeFactory.Date);
                break;
            default:
                base.InitializeParameter(dbParam, name, sqlType);
                break;
        }
    }
}

并更改connection.driver_class = nameSpace.OracleDataClientDriver2,AssemblyName

的值

答案 1 :(得分:0)

比继承oracle驱动程序类更简单的解决方案是在属性中添加type属性,值为“Date”。这是NHibernate类型。

<key-property name="TransactionDate" type="Date" column="TRN_DATE" />