From what I understand I need to end up with this
<property name="hibernate.generate_statistics">true</property>
on the session factory configuration, but I've no idea how to do that with fluent nhibernate.
Configuration.ExposeConfiguration(c => c.SetProperty("generate_statistics", "true"));
In NHibernate 3 to avoid strings:
Configuration.ExposeConfiguration(c => c.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true"));
Depending on how you're configuring Fluent NHibernate, the Database Configuration has a Raw method that you can use to specify settings that we haven't implemented yet.
SQLiteConfiguration.Standard
.Raw("hibernate.generate_statistics", "true");
Related
I've been working on a WCF service that uses fluent and syscache2. I've pretty much read every article on SO regarding my current dilemma; I've had no luck.
I am trying to set the expiration time for my second-level cache. Whatever value I set seems to be ignored and the default value of 5 minutes is used to expire the cache.
Fluent configuration:
Note: contextClass is just a descriptor class holding values passed to the configuration.
var cfg = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.Is(connectionString))
.ShowSql()
)
.Diagnostics(d => d.Enable())
.Cache(c => c
.UseQueryCache()
.ProviderClass(typeof(NHibernate.Caches.SysCache2.SysCacheProvider).AssemblyQualifiedName))
.Mappings(m => m
.FluentMappings
.AddFromAssembly(assembly))
.ExposeConfiguration(x =>
{
x.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, contextClass.Id);
x.SetProperty(NHibernate.Cfg.Environment.PrepareSql, contextClass.PrepareSql); //set prepare_sql true/false
x.SetProperty(NHibernate.Cfg.Environment.CacheDefaultExpiration, contextClass.ExpireL2Cache); //set default expiration in seconds
});
I also have the app.config file set up as following:
<configSections>
<section name="syscache" type="NHibernate.Caches.SysCache2.SysCacheSection, NHibernate.Caches.SysCache2"/>
</configSections>
<syscache>
<cache expiration="600" priority="5" />
</syscache>
There was a variant of the app.config which had a syscache section that used regions but that didn't work either.
Anyone have any suggestions on ideas?
Thanks
I've always used this without problems:
.ExposeConfiguration (cfg => {
cfg.Properties.Add ("expiration", "900");
})
Not sure if Properties.Add behaves any differently than the SetProperty call you're using though.
It seems like if you're using a newer version of NHibernate you can lean on the new extension methods in the NHibernate.Cfg namespace for this as well (this would replace your entire .Cache call in fluent)
.ExposeConfiguration (cfg => {
cfg.SessionFactory().Caching.Through<SysCacheProvider>().WithDefaultExpiration(900);
})
Doing some reading I found this:
cache.default_expiration or expiration (Int32): since NH Contrib 2.1 cache.default_expiration is the new setting name that should be used instead of expiration to specify number of seconds after which the cache item must be invalidated. Default value is 300 seconds. The old name is still supported for backward compatibility.
So the property name is probably not your issue (wondering now if the "expiration" key that I used was maybe specific to the memcache provider as well, though it seemed to work with syscache).
I have a need to fluently configure nhibernate in my S#arp application so that I can use a custom NHibernate.Search directory for each of my tenants in a multi-tenant app.
However I have googled for hours looking for a solution but can't seem to find anything current that works.
Thanks,
Paul
I haven't tried this myself, but AddConfiguration takes a dictionary of cfgProperties, which I guess you can pass the tenant specific hibernate.search.default.indexBase value to.
I had a look at this, adding the key as described above will cause a problem if you attempt to use CfgHelper.LoadConfiguration() since it will return null.
But you can configure NHSearch to use different directories for each factory using the factory key:
<nhs-configuration xmlns="urn:nhs-configuration-1.0">
<search-factory sessionFactoryName="YOUR_TENANT1_FACTORY_KEY">
<property name="hibernate.search.default.indexBase">~\IndexTenant1</property>
</search-factory>
<search-factory sessionFactoryName="YOUR_TENANT2_FACTORY_KEY">
<property name="hibernate.search.default.indexBase">~\Tenant2</property>
</search-factory>
</nhs-configuration>
If you are following instructions on
http://wiki.sharparchitecture.net/Default.aspx?Page=NHibSearch
You would need to change the method GetIndexDirectory to
private string GetIndexDirectory() {
INHSConfigCollection nhsConfigCollection = CfgHelper.LoadConfiguration();
string factoryKey = SessionFactoryAttribute.GetKeyFrom(this); // Change this with however you get the factory key for your tenants,
string property = nhsConfigCollection.GetConfiguration(factoryKey).Properties["hibernate.search.default.indexBase"];
var fi = new FileInfo(property);
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fi.Name);
}
I have been successfully using NHibernate, but now I am trying to move to Fluent NHibernate. I have created all of my mapping files and set up my session manager to use a Fluent Configuration. I then run my application and it runs successfully, but no data is returned.
There are no errors or any indication that there is a problem, but nothing runs.
when using NHibernate, if I don't set my hbm xml files as an embedded resource, this same thing happens. This makes me wonder what I have to set my Map classes to. Right now, they are just set to Compile, and they are compiled into the dll, which I can see by disassembling it.
Does anyone have any thoughts as to what may be happening here?
Thanks
private ISessionFactory GetSessionFactory()
{
return Fluently.Configure()
.Database(
IfxOdbcConfiguration
.Informix1000
.ConnectionString("Provider=Ifxoledbc.2;Password=mypass;Persist Security Info=True;User ID=myuser;Data Source=mysource")
.Dialect<InformixDialect1000>()
.ProxyFactoryFactory<ProxyFactoryFactory>()
.Driver<OleDbDriver>()
.ShowSql()
)
.Mappings(
x => x.FluentMappings.AddFromAssembly(System.Reflection.Assembly.GetExecutingAssembly())
//.ExportTo("C:\\mappings")
)
.BuildSessionFactory();
}
Does the executing assembly contain the fluent mapping classes? I would try:
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<MappedType>())
Where MappedType is a class that has a fluent mapping.
They should just be set to compile, that's fine. Nothing special needed here. The problem is most likely in your fluent configuration rather than the mapping.
I would like to set the default transaction level to ReadCommitted in my Fluent NHibernate configuration. If I were using XML mapping files, I could add a key to my config file:
<add key="hibernate.connection.isolation" value="ReadCommitted" />
but I can't figure out how to accomplish this with Fluent configuration.
Fluent NHibernate doesn't do anything with the transaction isolation, so the default will be whatever NHibernate defaults to. I don't know off the top of my head what that is.
We don't have an explicit method to set the isolation, but as it's just a configuration value you can use the Raw method to set the property.
MsSqlConfiguration.MsSql2008.Raw("connection.isolation", "isolation_level");
Source: https://web.archive.org/web/20100812054505/http://support.fluentnhibernate.org/discussions/help/45-default-isolation-level-for-transactions
You should specify isolation level, when calling: BeginTransaction on your Session object.
...
ISession session = SessionFactory.OpenSession();
session.BeginTransaction(IsolationLevel.ReadCommitted);
...
Please refer to: NHibernate transactions for more details.
With Fluent NHibernate v 2.x IsolationLevel() method can be used to globally set isolation level for transactions:
MsSqlConfiguration.MsSql2008
.IsolationLevel(System.Data.IsolationLevel.ReadCommitted)
I am trying to get Fluent nHibernate to generate mappings so I can take a look at the files and the sql.
My code is based on this post and on what I can glean from the documentation.
Fluent mapping - entities and classmaps in different assemblies
I am using the latest code from git.
Here’s my config code:
Configuration cfg = new Configuration();
var ft = Fluently.Configure(cfg);
//DbConnection by fluent
ft.Database
(
MsSqlConfiguration
.MsSql2008
.ConnectionString("……")
.ShowSql()
.UseReflectionOptimizer()
);
//get mapping files.
ft.Mappings(m =>
{
//set up the mapping locations
m.FluentMappings.AddFromAssemblyOf<Entity>()
.ExportTo(#"C:\temp");
m.Apply(cfg);
});
I also tried:
var sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration
.MsSql2008
.ShowSql()
.ConnectionString(“……"))
.Mappings(p => p.FluentMappings
.AddFromAssemblyOf<Entity>()
.ExportTo(#"c:\temp\"))
.BuildSessionFactory();
I have verified that the connection string is correct.
The issue is that no mapping files show up in the ExportTo folder and no sql code shows up in the output window or in the log file. No errors or exceptions are generated either.
I have no idea where to go from here.
Thank you in advance.
Rick
I think you have to actually spin up some objects to get the maps written out. If I remember correctly, this is not done at config time.