Thoughts about coding and books

You cant always close the persitencemanager when you are done with it.

I am used to close all recources when i am done with these but that seems not to be the case in app engine world.
Here is little method that gets all countries, adds these to List and closes PersistenceManager as its not needed anymore, we have all the data we need in List.

public List<Country> getCountries() {
PersistenceManager pm = PMF.get().getPersistenceManager();
String query = "select from " + Country.class.getName();
List<Country> countries = (List<Country>) pm.newQuery(query).execute();
pm.close();
return countries;
}

Countries are used in simple dataTable below, but when running this code app engine starts to complain “Object Manager has been closed”. To fix this issue pm.close(); can be commented out and hope that guys from google will close it eventually themselves. Google in its documentation does not recommend this though.

<h:dataTable value="#{countryBean.countries}" var="country">
<h:column>
</h:column>
</h:dataTable>

Probably appengine is lazy to actually get items from datastore to List but gets them only when particular item is used. So another way to fix it is to force appengine to force loading all entries from datastore by copying.

public List<Country> getCountries() {
PersistenceManager pm = PMF.get().getPersistenceManager();
String query = "select from " + Country.class.getName();
List<Country> countries = (List<Country>) pm.newQuery(query).execute();
List<Country> countries2=new ArrayList<Country>();
Collections.copy(countries, countries2);
pm.close();
return countries;
}

What i have not figured out yet is why all request take awful lot of CPU recource and how to reduce it. over 5000ms is like crazy

01-24 04:18AM 54.222 /faces/insertcountry.xhtml 200 930ms 5289cpu_ms 4570api_cpu_ms 0kb Mozilla/5.0 (X11; U; Linux x86_64; et; rv:1.9.1.7) Gecko/20100106 Ubuntu/9.10 (karmic) Firefox/3.5.7,gzip(gfe),gzip(gfe)
...
01-24 04:18AM 54.729 org.datanucleus.ObjectManagerImpl close: Outstanding nontx update being committed to datastore
An Error Occurred:
Object Manager has been closed
+- Stack Trace
 Read more about You cant always close the persitencemanager when you are done with it. ...

Appengine sample Guestbook Datastore works but yours fails.

When you first start experimenting with google app engine then you may get Exception below when trying to save data to datastore and checking out PercistenceManager for that. One thing to check it to see, if jdoconfig.xml in source packages and META-INF. Appengine example config is also included here.

<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">

<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass"
value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues" value="true"/>
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
</persistence-manager-factory>
</jdoconfig>

Read more about Appengine sample Guestbook Datastore works but yours fails.

JSF 2.0 dont like google appengine

You write nice simple JSF application and when you try to run it in appengine browser shows: HTTP ERROR: 404 NOT_FOUND RequestURI=/ Powered by jetty:// Stacktrace looks like: [java] The server is running at http://localhost:8080/ [java] 19.01.2010 19:42:14 com.google.apphosting.utils.jetty.JettyLogger warn [java] WARNING: failed com.google.apphosting.utils.jetty.DevAppEngineWebAppContext@6ee3849c{/,/home/user/NetBeansProjects/HelloWorld/build/web} [java] java.lang.NoClassDefFoundError: javax.naming.InitialContext is a restricted class. Please see the GoogleRead more about JSF 2.0 dont like google appengine[…]