[Gridsphere-users] Using Multiple Databases
Cary Palmer
gridsphere-users@gridlab.org
Thu, 17 Feb 2005 11:11:09 -0800 (PST)
--0-321000728-1108667469=:55341
Content-Type: text/plain; charset=us-ascii
Jason,
I'll tackle the JNDI issue later when I get some time.
On the multiple database issue I implemented the changes we discussed for the PersistenceManagerFactory and it turned out rather nice and it quite clean. I didn't even have to add a new constructor in the PersistenceManagerRdbmsImpl :-) What I did was simply add a new PersistenceManagerFactory method to implement a new database. So to implement a secondary database for a webapp I simply create a new directory for each new database under the /WEB-INF/perstence directory each with its own hibernate.properties file and hbm.xml file(s). This allows me to create multiple databases by simply creating a directory for each database. For example:
1. Create a new directory in the gridsphere webapp project:
\WEB-INF\persistence\catalog
2. Add a new hibernate.properties file (define the JDBC connection parms) and *.hbm.xml files for the secondary database:
\WEB-INF\persistence\catalog
3. In my webapp service instantiate a new PM for the new database as follows:
// our persistencemanager
private PersistenceManagerRdbms pm = null;
// our persistencemanager for the GeoState DB
private PersistenceManagerRdbms pm2 = null;
public void init(PortletServiceConfig config) throws PortletServiceUnavailableException {
// create the standard gridsphere persistencemanager for the portlet
this.pm = PersistenceManagerFactory.createPersistenceManagerRdbms("mywebapp");
log.info("Created the Portlet PM");
// create the persistencemanager for the GeoState db
this.pm = PersistenceManagerFactory.createPersistenceManagerRdbms("catalog","mywebapp");
log.info("Created the GeoSateService PM");
}
4. Define the service in PortletServices.xml.
5. The new createPersistenceManagerRdbms method in org.gridlab.gridsphere.core.persistence.PersistenceManagerFactory :
public static synchronized PersistenceManagerRdbms createPersistenceManagerRdbms(String dbname, String webappname) {
if (!databases.containsKey(dbname)) {
log.info("Creating new PM for :" + webappname + " for database "+ dbname);
ServletContext ctx = GridSphereConfig.getServletContext();
String path = ctx.getRealPath("../" + webappname + "/WEB-INF/persistence/" + dbname);
PersistenceManagerRdbms pm = new PersistenceManagerRdbmsImpl(path);
databases.put(dbname, pm);
}
return (PersistenceManagerRdbms) databases.get(dbname);
}
That's it. All I do from here is access the persistencemanager that I need from within the portlet level. Now I can get to multiple dbs with a minimal amount of work. The PersistenceManagerRdbmsImpl class requires no changes as it will create new sessions for each database.
Cary
Jason Novotny <novotny@aei.mpg.de> wrote:
Right-- I'm not sure about Hibernate + JNDI, this is the thread I
found on the subject http://www.hibernate.org/114.html it doesn't seem
very straightforward...
Jason
Cary Palmer wrote:
> Jason,
>
> Yeah, I think the simplest way is to do it this way passing the
> dbname on the new factory method and on to the new constructor. I
> figure passing a dbname and webappname give the ability to pick up the
> property file as well as having multiple databases:
>
> public static synchronized PersistenceManagerRdbms
> createPersistenceManagerRdbms(String dbname, String webappname) {
> if (!databases.containsKey(dbname)) {
> log.info("Creating new PM for :" + webappname + "
> database:"+ dbname);
> ServletContext ctx = GridSphereConfig.getServletContext();
> String path = ctx.getRealPath("../" + webappname +
> "/WEB-INF/persistence/");
> PersistenceManagerRdbms pm = new
> PersistenceManagerRdbmsImpl(path, dbname);
> databases.put(dbname, pm);
> }
> return (PersistenceManagerRdbms) databases.get(dbname);
> }
>
> I'd still like to get the Hibernate db connections working with JDNI?
>
> Cary
> */Jason Novotny /* wrote:
>
>
> Ok, you may have figured it out, but just to provide some details:
>
> The PersistenceManagerFactory provides 2 creation methods:
> + PersistenceManagerRdbms createGridSphereRdbms()
> + PersistenceManagerRdbms createPersistenceManagerRdbms(String
> webappname)
>
> the factory takes care of creating a PersistenceManagerRdbmsImpl
> internally and sticking it in a hash so that it can always be
> accessed
> via the factory create method.
>
> The PersistenceManagerRdbmsImpl as you see is just a really simple
> wrapper for the underlying Hibernate session factory.
>
> What I'm thinking is you may want to add both a new creator to the
> PersistenceManagerFactory and possibly a new constructor for
> PMRdbmsImpl
> (which would be used by the factory) for configuring a new db
> connection
> by specifying the properties file, etc.
>
> Jason
>
>
> Cary P! almer wrote:
>
> > Oliver,
> >
> > Looks like I'm going to modify the PM to handle multiple
> > sessionfactories to handle multiple databases. I'll try to keep it
> > simple, that way if the implementation is solid maybe someelse
> can use it.
> >
> > In regards to setting up a JNDI datasource I tried to change
> > my hibernate properties to use JNDI, but I'm not certain of the
> > plumbing in Gridsphere. According to the hibernate docs it should
> > work. In my ../persistence/hibernate.properties file I have the
> > following. Any ideas?
> >
> > ## MySQL
> > hibernate.dialect net.sf.hibernate.dialect.MySQLDialect
> > #Gridsphere method
> > #hibernate.connection.driver_class org.gjt.mm.mysql.Driver
> > #hibernate.connection.url
> > jdbc:mysql://localhost:3306/test?autoReconnect=true
> > #hibernate.connection.username test
> > #hibernate.connection.password test
> >
> > #JNDI datasource
> > hibernate.cglib.use_reflection_optimizer=true
> > hibernate.connection.datasource=java:comp/env/jdbc/test
> > hibernate.show_sql=true
> > hibernate.use_outer_join=false
> > hibernate.cache.use_query_cache=true
> > Cary
> >
> > */Oliver Wehrens /* wrote:
> >
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: SHA1
> >
> > Hi,
> >
> > On Feb 13, 2005, at 3:39 AM, cary palmer wrote:
> >
> > > Hello,
> > >
> > > That seems to work. But I think were I'm at odds is I need to
> > > integrate to multiple databases. Lets say one db is an ordering
> > db in
> > > Oracle and another is a sales db in DB2. I believe Hibernate
> > requires
> > > seperate SessionFatories for each datasource? As I see it I would
> > > need to extend the
> org.gridlab.gridsphere.core.persistence.hibernate
> > > package to allow multiple Hibernate SessionFactories?
> > >
> >
> > The GridSphere persistencemanager does not support (or better say
> > I did
> > not tested it) sharing the databases between different webapps. It
> > was
> > designed for each webapp have it's own database connection. I
> > tried to
> > make the GS persistencemanager very simple to use and just some few
> > commands. Of course this is a tradeoff between ease of use and
> things
> > you can do with it. In your case I would rather say use hibernate
> > directly (or extend the PM and send the patch ;-) ).
> >
> >
> > > Can I use c3po (connection pooling) for a portlet like gridsphere
> > > does? Also do you have an example of how I can configure a JNDI
> > > datasource for a portlet.
> >
> > I'm not sure what you mean by connection pooling for a portlet.
> >
> > I have never played around with JNDI, but maybe somebody else has an
> > idea...
> >
> >
> > Oliver
> >
> > - --
> > Oliver Wehrens
> > Albert Einstein Institut (AEI)
> > Max Planck Institut fuer Gravitationsphysik
> > Am Muehlenberg 1, 14476 Golm Germany
> > Tel: +49 170 785 1323
> >
> > Center for Computation and Technology (CCT)
> > Louisiana State University
> > 302 Johnston Hall
> > 70803 Baton Rouge, LA, USA
> > Tel: +1 225 284 0508
> >
> > AIM: oliverwehrens
> > -----BEGIN PGP SIGNATURE-----
> > Version: GnuPG v1.2.6 (Darwin)
> >
> > iD8DBQFCD2ctm9f1ZrJBmokRAl+cAJ982zVe4UBMU1yi9AIFzaOUoaAMZwCgzyHR
> > gWzAxN7CjEYmRQUvvv8+GaQ=
> > =Z6cA
> > -----END PGP SIGNATURE-----
> >
> > _______________________________________________
> > Gridsphere-users mailing list
> > Gridsphere-users@gridlab.org
> > https://www.gridlab.org/mailman/listinfo/gridsphere-users
> >
> >
> ------------------------------------------------------------------------
> > Do you Yahoo!?
> > Yahoo! Search presents - Jib Jab's 'Second Term'
> >
>
>
>
> _______________________________________________
> Gridsphere-users mailing list
> Gridsphere-users@gridlab.org
> https://www.gridlab.org/mailman/listinfo/gridsphere-users
>
> ------------------------------------------------------------------------
> Do you Yahoo!?
> Yahoo! Search presents - Jib Jab's 'Second Term'
>
_______________________________________________
Gridsphere-users mailing list
Gridsphere-users@gridlab.org
https://www.gridlab.org/mailman/listinfo/gridsphere-users
---------------------------------
Do you Yahoo!?
Yahoo! Search presents - Jib Jab's 'Second Term'
--0-321000728-1108667469=:55341
Content-Type: text/html; charset=us-ascii
<DIV>Jason,</DIV>
<DIV> </DIV>
<DIV>I'll tackle the JNDI issue later when I get some time.</DIV>
<DIV> </DIV>
<DIV>On the multiple database issue I implemented the changes we discussed for the PersistenceManagerFactory and it turned out rather nice and it quite clean. I didn't even have to add a new constructor in the PersistenceManagerRdbmsImpl :-) What I did was simply add a new PersistenceManagerFactory method to implement a new database. So to implement a secondary database for a webapp I simply create a new directory for each new database under the /WEB-INF/perstence directory each with its own hibernate.properties file and hbm.xml file(s). This allows me to create multiple databases by simply creating a directory for each database. For example:</DIV>
<DIV> </DIV>
<DIV>1. Create a new directory in the gridsphere webapp project:</DIV>
<DIV> </DIV>
<DIV>\WEB-INF\persistence\catalog</DIV>
<DIV> </DIV>
<DIV>2. Add a new hibernate.properties file (define the JDBC connection parms) and *.hbm.xml files for the secondary database:</DIV>
<DIV> </DIV>
<DIV>\WEB-INF\persistence\catalog</DIV>
<DIV> </DIV>
<DIV>3. In my webapp service instantiate a new PM for the new database as follows:</DIV>
<DIV><BR> // our persistencemanager<BR> private PersistenceManagerRdbms pm = null;</DIV>
<DIV> // our persistencemanager for the GeoState DB</DIV>
<DIV> private PersistenceManagerRdbms pm2 = null;</DIV>
<DIV> </DIV>
<DIV> public void init(PortletServiceConfig config) throws PortletServiceUnavailableException {</DIV>
<DIV> </DIV>
<DIV> // create the standard gridsphere persistencemanager for the portlet </DIV>
<DIV> this.pm = PersistenceManagerFactory.createPersistenceManagerRdbms("mywebapp");<BR> log.info("Created the Portlet PM");</DIV>
<DIV><BR> // create the persistencemanager for the GeoState db<BR> this.pm = PersistenceManagerFactory.createPersistenceManagerRdbms("catalog","mywebapp");<BR> log.info("Created the GeoSateService PM");<BR></DIV>
<DIV> }</DIV>
<DIV> </DIV>
<DIV>4. Define the service in PortletServices.xml.</DIV>
<DIV> </DIV>
<DIV>5. The new createPersistenceManagerRdbms method in org.gridlab.gridsphere.core.persistence.PersistenceManagerFactory :</DIV>
<DIV> </DIV>
<DIV> public static synchronized PersistenceManagerRdbms createPersistenceManagerRdbms(String dbname, String webappname) {<BR> if (!databases.containsKey(dbname)) {<BR> log.info("Creating new PM for :" + webappname + " for database "+ dbname);<BR> ServletContext ctx = GridSphereConfig.getServletContext();<BR> String path = ctx.getRealPath("../" + webappname + "/WEB-INF/persistence/" + dbname);<BR> PersistenceManagerRdbms pm = new PersistenceManagerRdbmsImpl(path);<BR> databases.put(dbname, pm);<BR> }<BR> return
(PersistenceManagerRdbms) databases.get(dbname);<BR> }</DIV>
<DIV> </DIV>
<DIV>That's it. All I do from here is access the persistencemanager that I need from within the portlet level. Now I can get to multiple dbs with a minimal amount of work. The PersistenceManagerRdbmsImpl class requires no changes as it will create new sessions for each database.</DIV>
<DIV> </DIV>
<DIV>Cary</DIV>
<DIV><BR><BR><B><I>Jason Novotny <novotny@aei.mpg.de></I></B> wrote:</DIV>
<BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #1010ff 2px solid"><BR>Right-- I'm not sure about Hibernate + JNDI, this is the thread I <BR>found on the subject http://www.hibernate.org/114.html it doesn't seem <BR>very straightforward...<BR><BR>Jason<BR><BR>Cary Palmer wrote:<BR><BR>> Jason,<BR>> <BR>> Yeah, I think the simplest way is to do it this way passing the <BR>> dbname on the new factory method and on to the new constructor. I <BR>> figure passing a dbname and webappname give the ability to pick up the <BR>> property file as well as having multiple databases:<BR>> <BR>> public static synchronized PersistenceManagerRdbms <BR>> createPersistenceManagerRdbms(String dbname, String webappname) {<BR>> if (!databases.containsKey(dbname)) {<BR>> log.info("Creating new PM for :" + webappname + " <BR>> database:"+ dbname);<BR>> ServletContext ctx = GridSphereConfig.getServletContext();<BR>> String path =
ctx.getRealPath("../" + webappname + <BR>> "/WEB-INF/persistence/");<BR>> PersistenceManagerRdbms pm = new <BR>> PersistenceManagerRdbmsImpl(path, dbname);<BR>> databases.put(dbname, pm);<BR>> }<BR>> return (PersistenceManagerRdbms) databases.get(dbname);<BR>> }<BR>><BR>> I'd still like to get the Hibernate db connections working with JDNI?<BR>> <BR>> Cary<BR>> */Jason Novotny <NOVOTNY@AEI.MPG.DE>/* wrote:<BR>><BR>><BR>> Ok, you may have figured it out, but just to provide some details:<BR>><BR>> The PersistenceManagerFactory provides 2 creation methods:<BR>> + PersistenceManagerRdbms createGridSphereRdbms()<BR>> + PersistenceManagerRdbms createPersistenceManagerRdbms(String<BR>> webappname)<BR>><BR>> the factory takes care of creating a PersistenceManagerRdbmsImpl<BR>> internally and sticking it in a hash so that it can always be<BR>> accessed<BR>> via the factory create method.<BR>><BR>> The
PersistenceManagerRdbmsImpl as you see is just a really simple<BR>> wrapper for the underlying Hibernate session factory.<BR>><BR>> What I'm thinking is you may want to add both a new creator to the<BR>> PersistenceManagerFactory and possibly a new constructor for<BR>> PMRdbmsImpl<BR>> (which would be used by the factory) for configuring a new db<BR>> connection<BR>> by specifying the properties file, etc.<BR>><BR>> Jason<BR>><BR>><BR>> Cary P! almer wrote:<BR>><BR>> > Oliver,<BR>> ><BR>> > Looks like I'm going to modify the PM to handle multiple<BR>> > sessionfactories to handle multiple databases. I'll try to keep it<BR>> > simple, that way if the implementation is solid maybe someelse<BR>> can use it.<BR>> ><BR>> > In regards to setting up a JNDI datasource I tried to change<BR>> > my hibernate properties to use JNDI, but I'm not certain of the<BR>> > plumbing in Gridsphere.
According to the hibernate docs it should<BR>> > work. In my ../persistence/hibernate.properties file I have the<BR>> > following. Any ideas?<BR>> ><BR>> > ## MySQL<BR>> > hibernate.dialect net.sf.hibernate.dialect.MySQLDialect<BR>> > #Gridsphere method<BR>> > #hibernate.connection.driver_class org.gjt.mm.mysql.Driver<BR>> > #hibernate.connection.url<BR>> > jdbc:mysql://localhost:3306/test?autoReconnect=true<BR>> > #hibernate.connection.username test<BR>> > #hibernate.connection.password test<BR>> ><BR>> > #JNDI datasource<BR>> > hibernate.cglib.use_reflection_optimizer=true<BR>> > hibernate.connection.datasource=java:comp/env/jdbc/test<BR>> > hibernate.show_sql=true<BR>> > hibernate.use_outer_join=false<BR>> > hibernate.cache.use_query_cache=true<BR>> > Cary<BR>> ><BR>> > */Oliver Wehrens /* wrote:<BR>> ><BR>> > -----BEGIN PGP SIGNED
MESSAGE-----<BR>> > Hash: SHA1<BR>> ><BR>> > Hi,<BR>> ><BR>> > On Feb 13, 2005, at 3:39 AM, cary palmer wrote:<BR>> ><BR>> > > Hello,<BR>> > ><BR>> > > That seems to work. But I think were I'm at odds is I need to<BR>> > > integrate to multiple databases. Lets say one db is an ordering<BR>> > db in<BR>> > > Oracle and another is a sales db in DB2. I believe Hibernate<BR>> > requires<BR>> > > seperate SessionFatories for each datasource? As I see it I would<BR>> > > need to extend the<BR>> org.gridlab.gridsphere.core.persistence.hibernate<BR>> > > package to allow multiple Hibernate SessionFactories?<BR>> > ><BR>> ><BR>> > The GridSphere persistencemanager does not support (or better say<BR>> > I did<BR>> > not tested it) sharing the databases between different webapps. It<BR>> > was<BR>> > designed for each webapp
have it's own database connection. I<BR>> > tried to<BR>> > make the GS persistencemanager very simple to use and just some few<BR>> > commands. Of course this is a tradeoff between ease of use and<BR>> things<BR>> > you can do with it. In your case I would rather say use hibernate<BR>> > directly (or extend the PM and send the patch ;-) ).<BR>> ><BR>> ><BR>> > > Can I use c3po (connection pooling) for a portlet like gridsphere<BR>> > > does? Also do you have an example of how I can configure a JNDI<BR>> > > datasource for a portlet.<BR>> ><BR>> > I'm not sure what you mean by connection pooling for a portlet.<BR>> ><BR>> > I have never played around with JNDI, but maybe somebody else has an<BR>> > idea...<BR>> ><BR>> ><BR>> > Oliver<BR>> ><BR>> > - --<BR>> > Oliver Wehrens<BR>> > Albert Einstein Institut (AEI)<BR>> > Max Planck
Institut fuer Gravitationsphysik<BR>> > Am Muehlenberg 1, 14476 Golm Germany<BR>> > Tel: +49 170 785 1323<BR>> ><BR>> > Center for Computation and Technology (CCT)<BR>> > Louisiana State University<BR>> > 302 Johnston Hall<BR>> > 70803 Baton Rouge, LA, USA<BR>> > Tel: +1 225 284 0508<BR>> ><BR>> > AIM: oliverwehrens<BR>> > -----BEGIN PGP SIGNATURE-----<BR>> > Version: GnuPG v1.2.6 (Darwin)<BR>> ><BR>> > iD8DBQFCD2ctm9f1ZrJBmokRAl+cAJ982zVe4UBMU1yi9AIFzaOUoaAMZwCgzyHR<BR>> > gWzAxN7CjEYmRQUvvv8+GaQ=<BR>> > =Z6cA<BR>> > -----END PGP SIGNATURE-----<BR>> ><BR>> > _______________________________________________<BR>> > Gridsphere-users mailing list<BR>> > Gridsphere-users@gridlab.org<BR>> > https://www.gridlab.org/mailman/listinfo/gridsphere-users<BR>> ><BR>> ><BR>>
------------------------------------------------------------------------<BR>> > Do you Yahoo!?<BR>> > Yahoo! Search presents - Jib Jab's 'Second Term'<BR>> ><BR>><BR>><BR>><BR>> _______________________________________________<BR>> Gridsphere-users mailing list<BR>> Gridsphere-users@gridlab.org<BR>> https://www.gridlab.org/mailman/listinfo/gridsphere-users<BR>><BR>> ------------------------------------------------------------------------<BR>> Do you Yahoo!?<BR>> Yahoo! Search presents - Jib Jab's 'Second Term' <BR>> <HTTP: evt="30648/*http://movies.yahoo.com/movies/feature/jibjabinaugural.html" us.rd.yahoo.com><BR><BR><BR><BR>_______________________________________________<BR>Gridsphere-users mailing list<BR>Gridsphere-users@gridlab.org<BR>https://www.gridlab.org/mailman/listinfo/gridsphere-users<BR></BLOCKQUOTE><p>
<hr size=1>Do you Yahoo!?<br>
Yahoo! Search presents - <a href="http://us.rd.yahoo.com/evt=30648/*http://movies.yahoo.com/movies/feature/jibjabinaugural.html">Jib Jab's 'Second Term'</a>
--0-321000728-1108667469=:55341--