无法使用 NHibernate 批量插入

发布于 2024-10-12 14:31:53 字数 2156 浏览 9 评论 0原文

我尝试将批量插入添加到我的应用程序中,但 Batcher 仍然是 NonBatchingBatcher,BatchSize 为 1。

这是使用 C#3、NH3RC1 和 MySql 5.1

我已将其添加到我的 SessionFactory 中

<property name="adonet.batch_size">100</property>

,我的代码非常像这样

var session = SessionManager.GetStatelessSession(type);
var tx = session.BeginTransaction();
session.Insert(instance);

我对相关实例使用 HILO 身份生成,但不是对数据库上的所有实例使用。 SessionFactory.OpenStatelessSession 不采用类型,因此它无法真正知道它可以对此类型进行批处理,还是......?

在深入研究 NHibernate 之后,我在 SettingsFactory.CreateBatcherFactory 中发现了一些可能会提供一些额外信息的

// It defaults to the NonBatchingBatcher
System.Type tBatcher = typeof (NonBatchingBatcherFactory);

// Environment.BatchStrategy == "adonet.factory_class", but I haven't
// defined this in my config file
string batcherClass = PropertiesHelper.GetString(Environment.BatchStrategy, properties, null);
if (string.IsNullOrEmpty(batcherClass))
{
    if (batchSize > 0)
    {
        // MySqlDriver doesn't implement IEmbeddedBatcherFactoryProvider,
        // so it still uses NonBatchingFactory
        IEmbeddedBatcherFactoryProvider ebfp = connectionProvider.Driver as IEmbeddedBatcherFactoryProvider;

内容 我的配置是否错误?

  <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="my application name">
      <property name="adonet.batch_size">100</property>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="connection.connection_string">my connection string
      </property>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
      <!-- To avoid "The column 'Reserved Word' does not belong to the table : ReservedWords" -->
      <property name="hbm2ddl.keywords">none</property>
    </session-factory>
  </hibernate-configuration>

I've tried adding bulk inserting to my application, but the Batcher is still NonBatchingBatcher with a BatchSize of 1.

This is using C#3, NH3RC1 and MySql 5.1

I've added this to my SessionFactory

<property name="adonet.batch_size">100</property>

And my code goes pretty much like this

var session = SessionManager.GetStatelessSession(type);
var tx = session.BeginTransaction();
session.Insert(instance);

I'm using HILO identity generation for the instances in question, but not for all instances on the database. The SessionFactory.OpenStatelessSession doesn't take a type, so it can't really know it can do batching on this type, or...?

After some digging into NHibernate, I found something in SettingsFactory.CreateBatcherFactory that might give some additional info

// It defaults to the NonBatchingBatcher
System.Type tBatcher = typeof (NonBatchingBatcherFactory);

// Environment.BatchStrategy == "adonet.factory_class", but I haven't
// defined this in my config file
string batcherClass = PropertiesHelper.GetString(Environment.BatchStrategy, properties, null);
if (string.IsNullOrEmpty(batcherClass))
{
    if (batchSize > 0)
    {
        // MySqlDriver doesn't implement IEmbeddedBatcherFactoryProvider,
        // so it still uses NonBatchingFactory
        IEmbeddedBatcherFactoryProvider ebfp = connectionProvider.Driver as IEmbeddedBatcherFactoryProvider;

Could my configuration be wrong?

  <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
    <session-factory name="my application name">
      <property name="adonet.batch_size">100</property>
      <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
      <property name="connection.connection_string">my connection string
      </property>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
      <!-- To avoid "The column 'Reserved Word' does not belong to the table : ReservedWords" -->
      <property name="hbm2ddl.keywords">none</property>
    </session-factory>
  </hibernate-configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度° 2024-10-19 14:31:53

我知道这个问题已经有一年了,但是有 一个 NuGet 包 添加了 MySQL 批处理功能NHibernate。它没有直接烘焙到 NHibernate 中的原因是该功能需要引用 MySQL.Data 程序集,而开发团队不希望存在依赖关系。

I know this question is a year old, but there is a NuGet package that adds MySQL batching functionality to NHibernate. The reason that it's not baked directly into NHibernate is that the functionality required a reference to the MySQL.Data assembly, and the dev team didn't want the dependency.

━╋う一瞬間旳綻放 2024-10-19 14:31:53

IIRC,批处理目前仅支持 Oracle 和 SqlServer。

与 NH 的几乎任何其他方面一样,这是可扩展的,因此您可以编写自己的 IBatcher/IBatcherFactory 并通过配置注入它们。

旁注:NH 当前版本是 3.0 GA。

IIRC, batching is currently supported for Oracle and SqlServer only.

As almost any other aspect of NH, this is extensible, so you can write your own IBatcher/IBatcherFactory and inject them via configuration.

Sidenote: current version of NH is 3.0 GA.

花海 2024-10-19 14:31:53

确实是老问题了,但是...让我们完全正确

批处理不起作用的另一个原因可能是使用无状态会话(如您的情况)。无状态会话不支持批处理。来自文档

insert()、update()和delete()操作定义
StatelessSession接口被认为是直接数据库
行级操作,导致立即执行 SQL
分别插入、更新或删除。因此,他们有很大不同
Save()、SaveOrUpdate() 和 Delete() 操作的语义
由ISession接口定义。

Really old question but...let's be completely correct

Another reason for batching not working can be use of stateless session (as in your case). Stateless session does not support batching. From documentation:

The insert(), update() and delete() operations defined by the
StatelessSession interface are considered to be direct database
row-level operations, which result in immediate execution of a SQL
INSERT, UPDATE or DELETE respectively. Thus, they have very different
semantics to the Save(), SaveOrUpdate() and Delete() operations
defined by the ISession interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文