Second Level Cache on Insert

43 weergaven
Naar het eerste ongelezen bericht

da...@raelyard.com

ongelezen,
6 nov. 2008 11:15:5606-11-2008
aan nhusers
I'm fairly new to NHibernate and this may be a bit of a silly
question, but it's not behaving as I expected and can't find a
definitive answer anywhere and don't see a test that addresses it, so
I'll just ask.

When I create a transient object and then save it to my session and
commit to my database, should it be added to my second level cache as
well. I was thinking that it would, but that doesn't seem to be the
case and I think maybe I'm missing something.

Thanks,
Dave

Ayende Rahien

ongelezen,
6 nov. 2008 20:22:5706-11-2008
aan nhu...@googlegroups.com
It does
Assuming you actually told NH to put it in the cahce in the config

da...@raelyard.com

ongelezen,
7 nov. 2008 11:04:4807-11-2008
aan nhusers
Thanks very much for the response, Oren.

I'm sorry if I'm just dense, but I think I do have it configured
properly. My objects do go into the second level cache when i
retrieve them, but not when I insert them. My inserts get into the
database, but not into the second level cache. If someone would tell
me what I'm missing I would be grateful. I've gone through all the
documentation I could find and the second level cache tests in the
NHibernate source and just can't come up with anything.

My NHibernate configuration is as follows:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</
property>
<property
name="connection.provider">NHibernate.Connection.DriverConnectionProvider</
property>
<property
name="connection.driver_class">NHibernate.Driver.SqlClientDriver</
property>
<property name="connection.connection_string_name">CacheTestObjects</
property>
<property name="default_schema">dbo</property>
<property
name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider,
NHibernate</property>
<mapping assembly="CachingTest.Objects" />
</session-factory>
</hibernate-configuration>

My mapping is this:
<class name="CacheTestObject" table="CacheTestObjects" lazy="false">
<cache usage="read-write"/>
<id name="ObjectId" column="ObjectId">
<generator class="identity" />
</id>
<property name="Name" />
<property name="Data" />
</class>

This is my code that is saving:
using (ISession session = sessionFactory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(testObject);
transaction.Commit();
session.Flush();
}
}



On Nov 6, 6:22 pm, "Ayende Rahien" <aye...@ayende.com> wrote:
> It doesAssuming you actually told NH to put it in the cahce in the config
>
>
>
> On Thu, Nov 6, 2008 at 6:15 PM, d...@raelyard.com <d...@raelyard.com> wrote:
>
> > I'm fairly new to NHibernate and this may be a bit of a silly
> > question, but it's not behaving as I expected and can't find a
> > definitive answer anywhere and don't see a test that addresses it, so
> > I'll just ask.
>
> > When I create a transient object and then save it to my session and
> > commit to my database, should it be added to my second level cache as
> > well.  I was thinking that it would, but that doesn't seem to be the
> > case and I think maybe I'm missing something.
>
> > Thanks,
> > Dave- Hide quoted text -
>
> - Show quoted text -

Ayende Rahien

ongelezen,
7 nov. 2008 11:08:1807-11-2008
aan nhu...@googlegroups.com
You are missing:
cache.use_second_level_cache = true

da...@raelyard.com

ongelezen,
7 nov. 2008 12:17:0707-11-2008
aan nhusers
Thanks for the suggestion, but I have added that to my configuration
and it doesn't make any difference. I still have successful caching
when I retrieve, but not when I save. If I need to have that
explicitly true (looks like default is true), why do the items I
retrieve get successfully placed into the second level cache? I'm
looking at the NHibernate.Cache output from log4net to see that I am
not caching on Save, that I have a cache miss and cache creation on
first Load, and that I have a cache hit on second Load.

Ayende Rahien

ongelezen,
7 nov. 2008 12:20:2307-11-2008
aan nhu...@googlegroups.com
Hm, it might be because of the possible changes in the data for insert in the DB

da...@raelyard.com

ongelezen,
7 nov. 2008 12:28:0607-11-2008
aan nhusers
perhaps related to using an identity column in sql server for for the
id? i'll explore what happens if i do something differently there.
Thanks again for you thoughts.

da...@raelyard.com

ongelezen,
7 nov. 2008 12:43:3007-11-2008
aan nhusers
That is exactly why it wasn't working. If i use "assigned" for the
generator class for the id for my class instead of "identity", it
works. My Save caches as expected. I'm going to mess around with it
a litttle more and try to understand it better. At this point it is
looking like I can't use an identity column for the id of my objects
if I want them to go into the second level cache when they are saved.
Is this correct?

Ayende Rahien

ongelezen,
7 nov. 2008 12:45:0707-11-2008
aan nhu...@googlegroups.com
And, in general, it is not recommended to use identity for other reasons.
usage of batching is one good reason

On Fri, Nov 7, 2008 at 7:44 PM, Ayende Rahien <aye...@ayende.com> wrote:
Probably, the issue is likely because we have to hit the DB to get the id anyway

Ayende Rahien

ongelezen,
7 nov. 2008 12:44:1607-11-2008
aan nhu...@googlegroups.com
Probably, the issue is likely because we have to hit the DB to get the id anyway

On Fri, Nov 7, 2008 at 7:43 PM, da...@raelyard.com <da...@raelyard.com> wrote:

da...@raelyard.com

ongelezen,
7 nov. 2008 13:09:0907-11-2008
aan nhusers
Understood. Thanks for your help.

Tapio Kulmala

ongelezen,
7 nov. 2008 13:26:5507-11-2008
aan nhusers
@Ayende

Are you familiar with the T-SQL 2005 OUTPUT statement? It could help
if we want to avoid extra roundtrips to the DB.

For example, try this

DECLARE @Test TABLE
( TestId [int] IDENTITY(1,1) NOT NULL
, Amount decimal(10,2)
, ts timestamp )

INSERT INTO @Test (Amount)
OUTPUT INSERTED.*
VALUES (100)

UPDATE @Test SET Amount = Amount * 10
OUTPUT INSERTED.*
WHERE TestId = 1

Could sql2005 dialect take advantage of this feature?

Tapio

On Nov 7, 7:44 pm, "Ayende Rahien" <aye...@ayende.com> wrote:
> Probably, the issue is likely because we have to hit the DB to get the id
> anyway
>

Fabio Maulo

ongelezen,
7 nov. 2008 13:28:1207-11-2008
aan nhu...@googlegroups.com
2008/11/7 Ayende Rahien <aye...@ayende.com>

And, in general, it is not recommended to use identity for other reasons.
usage of batching is one good reason

It is an hard and heavy battle to fight.

--
Fabio Maulo

Fabio Maulo

ongelezen,
7 nov. 2008 13:33:2707-11-2008
aan nhu...@googlegroups.com
That is one of the next feature of NH2.1.
It is identity using return parameter instead select statement.
The feature will be available for various dialects including ORACLE (in Oracle is a sequence generated by a trigger).

The feature will be active setting the property adonet.use_get_generated_keys="true" for "identity" generator.

BTW is better if you avoid it and avoid identity or identity-like generators.

Bye.
Fabio Maulo

2008/11/7 Tapio Kulmala <tapiok...@gmail.com>

Tapio Kulmala

ongelezen,
7 nov. 2008 13:39:2107-11-2008
aan nhusers
@Fabio

I didn't mean output parameters. OUTPUT statement can return the whole
row. It would also be handy if you are using optimistic locking with
timestamp fields.

Tapio

On Nov 7, 8:33 pm, "Fabio Maulo" <fabioma...@gmail.com> wrote:
> That is one of the next feature of NH2.1.It is identity using return
> parameter instead select statement.
> The feature will be available for various dialects including ORACLE (in
> Oracle is a sequence generated by a trigger).
>
> The feature will be active setting the property
> adonet.use_get_generated_keys="true" for "identity" generator.
>
> BTW is better if you avoid it and avoid identity or identity-like
> generators.
>
> Bye.
> Fabio Maulo
>
> 2008/11/7 Tapio Kulmala <tapiokulm...@gmail.com>

Ayende Rahien

ongelezen,
7 nov. 2008 13:56:2907-11-2008
aan nhu...@googlegroups.com
Will this be on by default?

Fabio Maulo

ongelezen,
7 nov. 2008 13:58:4407-11-2008
aan nhu...@googlegroups.com
2008/11/7 Ayende Rahien <aye...@ayende.com>

Will this be on by default?

So far no. Only setting adonet.use_get_generated_keys="true"

--
Fabio Maulo

Jon Palmer

ongelezen,
7 nov. 2008 16:00:1307-11-2008
aan nhu...@googlegroups.com

I’ve seen this comment before but never found a good explanation of why identity is not recommended. Is there a good write up of the problem and what the various solutions are?

 

Thanks

Jon

Gustavo Ringel

ongelezen,
7 nov. 2008 16:07:2507-11-2008
aan nhu...@googlegroups.com
Suppose in the same session you save an entity, update it and then delete it...without commitint in the middle. What would you expect to happen?

I will expect NH to do nothing with the DB. Because the entity was never persisted (by a commit)

This is possible if you don't use Identity. If you use identity NH will go for the ID immediataly upon save...

There are other reasons and some cascade options will cause the save even without identity...but you should strive to avoid forcing DB Hits...

Gustavo.

Ayende Rahien

ongelezen,
7 nov. 2008 16:12:2807-11-2008
aan nhu...@googlegroups.com
When you associate an entity with identity to the session (such as by calling Save() ).
NH has to go to the DB immediately, in order to generate the entity id.

On Fri, Nov 7, 2008 at 11:00 PM, Jon Palmer <jpa...@contactnetworks.com> wrote:

Fabio Maulo

ongelezen,
7 nov. 2008 16:14:1307-11-2008
aan nhu...@googlegroups.com
Hi Jon.
Do you have some write where somebody explain why identity is so good ?

Using ORM identity is not recommended because the only way to know the ID of the entity is writing the entity itself in a safe state and hopefully inside a transaction and, after the INSERT INTO, read, in some way, the result to know the ID.
There are various other reasons... for example try a migration.
Take a look here

Please if you find something where somebody explain why identity is so good instead a simple int, let me know.

2008/11/7 Jon Palmer <jpa...@contactnetworks.com>



--
Fabio Maulo

Ayende Rahien

ongelezen,
7 nov. 2008 16:16:0907-11-2008
aan nhu...@googlegroups.com
identity is good if you want an always increasing number and you want the DB to handle that.
As an example, when you have ORM and non ORM writing to the DB
In addition to that, having a guaranteed DB based always increasing number means that you get a good indexing.

something like hilo strategy deals with that, of course, but that is another matter.

Fabio Maulo

ongelezen,
7 nov. 2008 16:30:1407-11-2008
aan nhu...@googlegroups.com
2008/11/7 Ayende Rahien <aye...@ayende.com>

identity is good if you want an always increasing number and you want the DB to handle that.

;) 
I heard the same reason before because 1,2,3,5,6,9,10....100,104
have a BIG difference with  10001,10002...10101,10102,10103...51101,51103,51104

and you can imagine how many times some human are going to check the sequence of IDs and, better more, why some human must go to look the IDs especially when: "should have absolutely no business meaning whatsoever. Nada. Zip. Zilch. Zero"

BTW as I said, my friend, a very hard battle to fight.

--
Fabio Maulo

Steve Wagner

ongelezen,
23 nov. 2008 15:26:4123-11-2008
aan nhu...@googlegroups.com
I think this text would be very helpful when it would be inserted as
warning to the NH documentation.

Fabio Maulo schrieb:
> Hi Jon.Do you have some write where somebody explain why identity is so good


> ?
>
> Using ORM identity is not recommended because the only way to know the ID of
> the entity is writing the entity itself in a safe state and hopefully inside
> a transaction and, after the INSERT INTO, read, in some way, the result to
> know the ID.
> There are various other reasons... for example try a migration.
> Take a look here
> http://nhusers.googlegroups.com/web/WhitePaper_Mapping_Objects_To_Relational_Databases.pdf
>
> Please if you find something where somebody explain why identity is so good
> instead a simple int, let me know.
>
> 2008/11/7 Jon Palmer <jpa...@contactnetworks.com>
>
>> I've seen this comment before but never found a good explanation of why
>> identity is not recommended. Is there a good write up of the problem and
>> what the various solutions are?
>>
>>
>>
>> Thanks
>>
>> Jon
>>
>>
>>

>> *From:* nhu...@googlegroups.com [mailto:nhu...@googlegroups.com] *On
>> Behalf Of *Fabio Maulo
>> *Sent:* Friday, November 07, 2008 8:28 AM
>> *To:* nhu...@googlegroups.com
>> *Subject:* [nhusers] Re: Second Level Cache on Insert

karron

ongelezen,
24 nov. 2008 02:11:3224-11-2008
aan nhu...@googlegroups.com
I'm wondering which algorithm is recommend? As you said, identity is
not so good. The remains are only Guid related algorithm. Oracle
doesn't support guid data type, so may we choose uuid.hex? and choose
guid.comb when the database is ms sql?
--
Regards,
Karron

Gustavo Ringel

ongelezen,
24 nov. 2008 02:14:2124-11-2008
aan nhu...@googlegroups.com
you have also hilo. You can read about it on the documents in this list. I think it is a Steve Ambler articles that Fabio posted.

Gustavo.

karron

ongelezen,
24 nov. 2008 04:14:0124-11-2008
aan nhu...@googlegroups.com
Thank you, It is a very useful white paper! I'm not familiar with
hilo before. After I read the paper, I had a try for hilo. It looks
good. But there is a little odd. When I used hilo, nh didn't print the
insert statements(But it printed select, update, delete statements),
when I switched back to native, it printed all sql statements.


On Mon, Nov 24, 2008 at 3:14 PM, Gustavo Ringel
<gustavo...@gmail.com> wrote:
> you have also hilo. You can read about it on the documents in this list. I
> think it is a Steve Ambler articles that Fabio posted.
>
> Gustavo.
>

--
Regards,
Karron

karron

ongelezen,
24 nov. 2008 04:47:2024-11-2008
aan nhu...@googlegroups.com
another question, the hilo generator will update and select the
hibernate_unique_key table to get the value for id, is there
concurrency problem in high traffic application?

--
Regards,
Karron

Fabio Maulo

ongelezen,
24 nov. 2008 05:09:1724-11-2008
aan nhu...@googlegroups.com
2008/11/24 karron <karr...@gmail.com>


another question, the hilo generator will update and select the
hibernate_unique_key table to get the value for id, is there
concurrency problem in high traffic application?

try to broke it.
--
Fabio Maulo

Ayende Rahien

ongelezen,
24 nov. 2008 05:33:2224-11-2008
aan nhu...@googlegroups.com
No, there isn't, because it is not done for each item, but for each range.
That is, I am incrementing the value by one, but I have a range of 100 ( by default, IIRC ) to work on.
Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten