Skip to content

Instantly share code, notes, and snippets.

@ArtemGr
Created April 14, 2009 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtemGr/95253 to your computer and use it in GitHub Desktop.
Save ArtemGr/95253 to your computer and use it in GitHub Desktop.
Derby via JDO bug.
package fastcms.betdvxq4;
// NO_AUTO_CLASS
import javax.jdo.annotations._
@PersistenceCapable {val identityType = IdentityType.APPLICATION}
class Persistent {@PrimaryKey var name: String = null; var value: String = null}
@PersistenceAware
class main (_tr: ru.glim.cache.Trappings) {
val JDO_FACTORY = withCL {
val jdoProperties = new java.util.HashMap[String, String]
// http://www.datanucleus.org/products/accessplatform_1_1/rdbms/support.html
jdoProperties.put ("datanucleus.ConnectionDriverName", "org.apache.derby.jdbc.EmbeddedDriver")
jdoProperties.put ("datanucleus.ConnectionURL", "jdbc:derby:" + _tr.cache.getOwner + _tr.id + ".derby" + ";create=true") // ;upgrade=true
jdoProperties.put ("datanucleus.autoCreateSchema", "true")
jdoProperties.put ("datanucleus.autoCreateTables", "true")
jdoProperties.put ("datanucleus.autoCreateColumns", "true")
javax.jdo.JDOHelper.getPersistenceManagerFactory (jdoProperties)
}
def withCL[T] (fun: =>T): T = {
val oldClassLoader = Thread.currentThread.getContextClassLoader
try {
Thread.currentThread.setContextClassLoader (getClass.getClassLoader)
fun
} finally {
Thread.currentThread.setContextClassLoader (oldClassLoader)
}
}
def main = withCL {
val cms = ru.bizlink.base.cms.Cms.getInstance
val jdoManager = JDO_FACTORY.getPersistenceManager
val tx = jdoManager.currentTransaction; try {
tx.begin
val query = jdoManager.newQuery (classOf[Persistent], "name == \"foo\"")
def results = query.execute.asInstanceOf[java.util.List[Persistent]]
if (!results.isEmpty) cms.writebr ("have: " + results.get(0).value) else {
val ps = new Persistent; ps.name = "foo"; ps.value = "bar"
jdoManager.makePersistent (ps)
cms.writebr ("new: " + results.get(0).value)}
tx.commit
} finally {
if (tx.isActive) tx.rollback; jdoManager.close
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment