1

I'm trying to combine sprig.net with ASP.NET MVC3, using PostgreSQL as DB. I succeeded to do this with MySQL before. Here are my 2 config files:

1) web.config:

<configuration>
<configSections>
<sectionGroup name="spring">
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler,     Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web" />
</sectionGroup>
</configSections>
<!-- spring context -->
<spring>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
</parsers>
<context>
<resource uri="~/spring-dao.xml" />
</context>
</spring>
....

2) spring-dao.xml (for MySQL DB) was:

 <db:provider id="DbProvider" provider="MySql.Data.MySqlClient"
 ConnectionString="Server=localhost;Database=db_movies;Uid=root;Pwd=1234;"/>
 <object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject,
 Spring.Data.NHibernate32">
 <property name="DbProvider" ref="DbProvider"/> ...

NB: I've referenced the Npgsql.dll.

Do I need another .DLL and how can I change the <db:provider .../> to use Postgres as provider?

I looked for examples or tutorials via google, but I didn't find any.

1 Answer 1

1

Find below a working configuration of NHibernate32 and Npgsql-2.0 (2.0.11.92) for Spring.Net 1.3.2:

<db:provider id="PqSqlDbProvider" provider="Npgsql-2.0" connectionString="Server=localhost;Port=5432;User Id=nhibernate;Password=123456789;Database=Xxxxxxxx;" />

<object name="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32" dependency-check="none">
  <property name="DbProvider" ref="PqSqlDbProvider"/>
  <property name="ExposeTransactionAwareSessionFactory" value="true" />
  <property name="MappingAssemblies">
    <list>
      <value>Xxxxxxxx</value>
    </list>
  </property>

  <property
    name="HibernateProperties">
    <dictionary>        
      <entry key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <entry key="connection.driver_class" value="NHibernate.Driver.NpgsqlDriver" />
      <entry key="dialect" value="NHibernate.Dialect.PostgreSQLDialect" />
      <entry key="hbm2ddl.auto" value="create-drop"/>
    </dictionary>
  </property>
</object>

<object
  name="HibernateTransactionManager"
  type="Spring.Data.NHibernate.HibernateTransactionManager, Spring.Data.NHibernate32"
  dependency-check="none">
  <property name="DbProvider" ref="PqSqlDbProvider"/>
  <property name="SessionFactory" ref="SessionFactory"/>
</object>

<!-- Attribute driven transaction manager. -->
<tx:attribute-driven transaction-manager="HibernateTransactionManager"/>

You also need to amend the web.config to load the proper .dll instead of the one referenced by Spring:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-65535.65535.65535.65535" newVersion="2.0.11.92" />
    </dependentAssembly>      
  </assemblyBinding>
</runtime>
Sign up to request clarification or add additional context in comments.

5 Comments

Hi AndreasKI. Thank you for your answer. I've updated my web.config and spring-dao.xml as you suggest. but i'm facing a new error : Resource handler for the 'web' protocol is not defined. when i try to call ContextRegistry.GetContext(); Here is the line that causes error : IMovieServices service = (IMovieServices)ContextRegistry.GetContext().GetObject("MovieService");
This is the MovieService declaration in the spring-dao.xml : <object id="MovieService" type="MvcTest.Services.MovieServicesImpl, MvcTest" init-method="init" destroy-method="destroy"> <property name="HibernateTemplate" ref="HibernateTemplate"/> </object>
When do you call IMovieServices service = (IMovieServices)ContextRegistry.GetContext().GetObject("MovieService");? Imho the context is not ready e.g. you are calling before Application (global.asax) Init is executed. Anyway you should not directly access the Context. stackoverflow.com/questions/9807355/…
Hi AndreasKI. Thank you for your answer. I use this call in a controller. I never initialized the context manually. By adding the IIS 7 config, as explained in the thread above(that you gave me), all went fine. It works now, despite i've IIS 8.0 installed !! THANK U A LOT !!
@IbrahimaMouctarDiallo: If Andreas' answer solved your question, please consider accepting it. More info here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.