use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.
the class MyApp method defineDynamicPersistentUnit.
public static void defineDynamicPersistentUnit() {
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
pumd.addProperty("javax.jdo.option.ConnectionURL", "xml:file:myfile_dynamicPMF.xml");
pumd.addProperty("datanucleus.schema.autoCreateAll", "true");
pumd.addProperty("datanucleus.xml.indentSize", "4");
pmf = new JDOPersistenceManagerFactory(pumd, null);
pm = pmf.getPersistenceManager();
}
use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.
the class GuideToJDOIntegrationTest method givenProduct_WhenQueryThenExist.
@Test
public void givenProduct_WhenQueryThenExist() {
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
pumd.addClassName("com.baeldung.jdo.Product");
pumd.setExcludeUnlistedClasses();
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true");
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Product product = new Product("Tablet", 80.0);
pm.makePersistent(product);
Product product2 = new Product("Phone", 20.0);
pm.makePersistent(product2);
Product product3 = new Product("Laptop", 200.0);
pm.makePersistent(product3);
tx.commit();
} catch (Throwable thr) {
fail("Failed test : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.close();
PersistenceManagerFactory pmf2 = new JDOPersistenceManagerFactory(pumd, null);
PersistenceManager pm2 = pmf2.getPersistenceManager();
Transaction tx2 = pm2.currentTransaction();
try {
tx2.begin();
@SuppressWarnings("rawtypes") Query q = pm2.newQuery("SELECT FROM " + Product.class.getName() + " WHERE price == 200");
@SuppressWarnings("unchecked") List<Product> products = (List<Product>) q.execute();
for (Product p : products) {
assertEquals("Laptop", p.name);
}
tx2.commit();
} finally {
if (tx2.isActive()) {
tx2.rollback();
}
pm2.close();
}
}
use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tutorials by eugenp.
the class GuideToJDOIntegrationTest method givenProduct_WhenNewThenPerformTransaction.
@Test
public void givenProduct_WhenNewThenPerformTransaction() {
PersistenceUnitMetaData pumd = new PersistenceUnitMetaData("dynamic-unit", "RESOURCE_LOCAL", null);
pumd.addClassName("com.baeldung.jdo.Product");
pumd.setExcludeUnlistedClasses();
pumd.addProperty("javax.jdo.option.ConnectionDriverName", "org.h2.Driver");
pumd.addProperty("javax.jdo.option.ConnectionURL", "jdbc:h2:mem:mypersistence");
pumd.addProperty("javax.jdo.option.ConnectionUserName", "sa");
pumd.addProperty("javax.jdo.option.ConnectionPassword", "");
pumd.addProperty("datanucleus.autoCreateSchema", "true");
PersistenceManagerFactory pmf = new JDOPersistenceManagerFactory(pumd, null);
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
for (int i = 0; i < 100; i++) {
String nam = "Product-" + i;
Product productx = new Product(nam, (double) i);
pm.makePersistent(productx);
}
tx.commit();
} catch (Throwable thr) {
fail("Failed test : " + thr.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
pmf.close();
}
use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.
the class PersistenceManagerFactoryTest method testSerialize.
public void testSerialize() {
try {
Object id = null;
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person p = new Person(101, "Fred", "Flintstone", "fred.flintstone@warnerbros.com");
pm.makePersistent(p);
tx.commit();
id = pm.getObjectId(p);
} catch (Exception e) {
LOG.error("Exception persisting object", e);
fail("Exception persisting object : " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
// Serialize the current PMF
byte[] bytes = null;
try {
bytes = TestHelper.serializeObject(pmf);
} catch (RuntimeException re) {
LOG.error("Error serializing PMF", re);
fail("Error in serialization : " + re.getMessage());
}
// Deserialise the PMF
JDOPersistenceManagerFactory pmf = null;
try {
pmf = (JDOPersistenceManagerFactory) TestHelper.deserializeObject(bytes);
} catch (RuntimeException re) {
LOG.error("Error deserializing PMF", re);
fail("Error in deserialization : " + re.getMessage());
}
assertNotNull(pmf);
assertNotNull(pmf.getNucleusContext());
// Retrieve the object using the deserialised PM(F)
pm = pmf.getPersistenceManager();
tx = pm.currentTransaction();
try {
tx.begin();
Person p = (Person) pm.getObjectById(id);
assertNotNull(p);
assertEquals("Fred", p.getFirstName());
assertEquals("Flintstone", p.getLastName());
assertEquals("fred.flintstone@warnerbros.com", p.getEmailAddress());
Person p2 = new Person(102, "Barney", "Rubble", "barney.rubble@warnerbros.com");
pm.makePersistent(p2);
pm.flush();
tx.rollback();
} catch (Exception e) {
LOG.error("Exception retrieving object with deserialised PMF", e);
fail("Exception retrieving object with deserialised PMF: " + e.getMessage());
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
} finally {
clean(Person.class);
}
}
use of org.datanucleus.api.jdo.JDOPersistenceManagerFactory in project tests by datanucleus.
the class PersistenceManagerFactoryTest method testServerTimeZoneID.
/**
* Test for serverTimeZoneID setting.
*/
public void testServerTimeZoneID() {
PersistenceManagerFactory pmf = null;
// Try setting the serverTimeZoneID to an invalid value
try {
Properties userProps = new Properties();
userProps.setProperty("javax.jdo.option.ServerTimeZoneID", "JPOX_CENTRAL_TIMEZONE");
pmf = getPMF(1, userProps);
fail("Expected a JDOUserException when setting the ServerTimeZoneID to an invalid value but worked!");
} catch (JDOFatalUserException jdoe) {
// Expected
} finally {
if (pmf != null && !pmf.isClosed()) {
pmf.close();
}
}
// Try setting it to a valid value and make sure it is retained
try {
Properties userProps = new Properties();
userProps.setProperty("javax.jdo.option.ServerTimeZoneID", "UTC");
pmf = getPMF(1, userProps);
assertEquals("ServerTimeZoneID was not set correctly", ((JDOPersistenceManagerFactory) pmf).getServerTimeZoneID(), "UTC");
} catch (Exception e) {
fail("Exception thrown when setting the ServerTimeZoneID to UTC");
} finally {
if (pmf != null && !pmf.isClosed()) {
pmf.close();
}
}
// Try leaving it unset and check the value
try {
pmf = getPMF(1, null);
assertEquals("ServerTimeZoneID was not set correctly", ((JDOPersistenceManagerFactory) pmf).getServerTimeZoneID(), null);
} catch (Exception e) {
fail("Exception thrown when setting the ServerTimeZoneID to UTC");
} finally {
if (pmf != null && !pmf.isClosed()) {
pmf.close();
}
}
}
Aggregations