c# - Nhibernate Multidatabase Connection -
we appreciate hep following problem.
we developping application using mvc4 , nhibernate.
the application must handle multidatabase connections.
each client has own database, there initial login form connects database clients, once credentials checked must stablish connection especific client database.
we have been working on problem severals days , solution on wich working till now:
we have created property on global.asax sessionfactory dictionary. every time client login add new sessionfactory dictionary.
we have created actionfilter control on every action bind , unbind of nhibernate session, follows:
public class sessionperrequest : actionfilterattribute { public override void onactionexecuting(actionexecutingcontext filtercontext) { boreas.subsistemas.cliente.vo.nuestro_clientevo cliente = (boreas.subsistemas.cliente.vo.nuestro_clientevo)filtercontext.httpcontext.session["cliente"]; nhibernate.isession session = mvcapplication.getsession(cliente).opensession(); session.begintransaction(); currentsessioncontext.bind(session); } public override void onresultexecuted(resultexecutedcontext filtercontext) { boreas.subsistemas.cliente.vo.nuestro_clientevo cliente = (boreas.subsistemas.cliente.vo.nuestro_clientevo)filtercontext.httpcontext.session["cliente"]; nhibernate.isession session = currentsessioncontext.unbind(mvcapplication.getsession(cliente)); if (session != null) { if (session.transaction.isactive) { try { session.transaction.commit(); } catch { session.transaction.rollback(); } } session.close(); } mvcapplication.removesession(cliente); } }
and methods on global.asax create, delete , return session factory each client:
public static void addsession(boreas.subsistemas.cliente.vo.nuestro_clientevo cliente) { if (!dictionarysessionfactory.containskey(cliente)) { isessionfactory session; var nhibernateconiguration = new nhibernate.cfg.configuration(); nhibernateconiguration.setproperty("connection.connection_string", cliente.basedatosclienteboreas); nhibernateconiguration.configure(); sessionfactory = nhibernateconiguration.buildsessionfactory(); } } public static void removesession(boreas.subsistemas.cliente.vo.nuestro_clientevo cliente) { if (cliente != null) { if (dictionarysessionfactory.containskey(cliente)) { dictionarysessionfactory.remove(cliente); } } } public static isessionfactory getsession(boreas.subsistemas.cliente.vo.nuestro_clientevo cliente) { isessionfactory session; if (cliente == null) { var nhibernateconiguration = new nhibernate.cfg.configuration(); nhibernateconiguration.setproperty("connection.connection_string", "data source=benito-pc\\wincodice;initial catalog=clientesboreasrutas;user id=sa;password=2144;"); nhibernateconiguration.configure(); session = nhibernateconiguration.buildsessionfactory(); //dictionarysessionfactory.add(null, session); return session; } else { if (dictionarysessionfactory.containskey(cliente)) { return dictionarysessionfactory[cliente]; } else { var nhibernateconiguration = new nhibernate.cfg.configuration(); nhibernateconiguration.setproperty("connection.connection_string", cliente.basedatosclienteboreas); nhibernateconiguration.configure(); session = nhibernateconiguration.buildsessionfactory(); dictionarysessionfactory.add(cliente, session); return sessionfactory; } } }
the problem application returns nhibernate.hibernateexception "no session bound current context"
we appreciate solve problem, , interesting listen other proper way implement multi database connections nhibernate , mvc4.
thank u reading post
in onresultexecuted method doesn't line:
nhibernate.isession session = currentsessioncontext.unbind(mvcapplication.getsession(cliente));
unbinds transaction current context before being commited? line should come once done session.
before commit transaction should retrieve session (by calling getcurrentsession() on the sessionfactory) commit transaction on , unbind it.
Comments
Post a Comment