Jdo: Programmatically create multiple persistence units in a DataNucleus

I have two different data sources for which I need two different PersistenceManagerFactory. I can always get this by writing a file persistence.xml. But I want this to be presented programmatically. Although the second data source remains relatively unchanged, the first data source may have additions to it through plugins. These plugins may come with one or more annotated JDO classes. A persitance.xmlwould not be such a good idea here because I want them to load at runtime.

In Hibernate (and with JPA), this would be possible by creating a configuration object and adding all annotated classes to it. Whenever I see a new downloadable plugin, I can always disable SessionFactoryand reload it with additional classes from the plugin by looking at the annotation @Entity.

Is there a similar way to do this in DataNucleus / JDO?

I tried to find it on Google, but everything I did was on the DataNucleus website , explaining how to write a file persitence.xml.

+3
source share
1 answer

The code below in Scala demonstrates how you can dynamically create PersistenceManager. You just need to fill out the card and transfer it to JDOHelper#getPersistenceManagerFactory.

private val pu  = props(db.driver, db.url, db.username, db.password)
private val pmf = JDOHelper.getPersistenceManagerFactory(pu.asJava)
private val pm  = pmf.getPersistenceManager.asInstanceOf[JDOPersistenceManager]

, , , ​​ H2, MongoDB PostgreSQL:

  def props(driver: String, url: String, username: String, password: String): Map[String, Any] =
    driver match {
      case "org.h2.Driver" =>
        Map[String, Any](
          "javax.jdo.option.Mapping"                 -> "h2",
          "datanucleus.schema.autoCreateAll"         -> "true",
          "javax.jdo.PersistenceManagerFactoryClass" -> "org.datanucleus.api.jdo.JDOPersistenceManagerFactory",
          "javax.jdo.option.ConnectionDriverName"    -> driver,
          "javax.jdo.option.ConnectionURL"           -> url,
          "javax.jdo.option.ConnectionUserName"      -> username,
          "javax.jdo.option.ConnectionPassword"      -> password
        )

      case "org.postgresql.Driver" =>
        Map[String, Any](
          "datanucleus.schema.autoCreateAll"         -> "true",
          "javax.jdo.PersistenceManagerFactoryClass" -> "org.datanucleus.api.jdo.JDOPersistenceManagerFactory",
          "javax.jdo.option.ConnectionDriverName"    -> driver,
          "javax.jdo.option.ConnectionURL"           -> url,
          "javax.jdo.option.ConnectionUserName"      -> username,
          "javax.jdo.option.ConnectionPassword"      -> password,
          "javax.jdo.option.RetainValues"            -> "true",
          "javax.jdo.option.RestoreValues"           -> "true",
          "javax.jdo.option.Optimistic"              -> "true",
          "javax.jdo.option.NontransactionalWrite"   -> "false",
          "javax.jdo.option.NontransactionalRead"    -> "true",
          "javax.jdo.option.Multithreaded"           -> "true",
          "javax.jdo.option.IgnoreCache"             -> "false"
        )

      case "mongodb.jdbc.MongoDriver" =>
        Map[String, Any](
          "javax.jdo.option.Mapping"                 -> "mongo",
          "datanucleus.schema.autoCreateAll"         -> "true",
          "javax.jdo.PersistenceManagerFactoryClass" -> "org.datanucleus.api.jdo.JDOPersistenceManagerFactory",
          "javax.jdo.option.ConnectionDriverName"    -> driver,
          "javax.jdo.option.ConnectionURL"           -> url,
          "javax.jdo.option.ConnectionUserName"      -> username,
          "javax.jdo.option.ConnectionPassword"      -> password
        )

      case _ => throw new IllegalArgumentException(s"unknown driver %s".format(driver))
    }
0

All Articles