views:

25

answers:

0

Hello all!

Is this the right aproch while using the datastore under Google App Engine? I have this super-class with the right includes:


package gae.google.db.object;
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")

@Inheritance(strategy = InheritanceStrategy.NEW_TABLE)
public class User{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key userIdKey;  
        @Persistent
    private String name;
        public Key getUserIdKey() {return userIdKey;} 
        public void setUserIdKey(Key userIdKey) {this.userIdKey = userIdKey;}
        public String getName(){return name;}
        public void setName(String name) {this.name = name;}
}

and when storing it I use


package gae.google.test.db;

import javax.jdo.PersistenceManagerFactory;
import javax.jdo.JDOHelper;

public class PMFactory {
    private static final PersistenceManagerFactory pmfInstance = JDOHelper.getPersistenceManagerFactory("transactions-optional");
    private PMFactory() {}
    public static PersistenceManagerFactory get() {
        return pmfInstance;
    }
}

and for storing and getting data I use


package gae.google.test.db;

import gae.google.db.object.User;
import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;

public class SaveToDB {
    private static PersistenceManager pm = PMFactory.get().getPersistenceManager();

    public static User saveUser(User user) {
        if(pm.isClosed()) {
            pm = PMFactory.get().getPersistenceManager();
        }
        try {
            pm.currentTransaction().begin();
            pm.makePersistent(user);    
            pm.detachCopy(user);
            pm.currentTransaction().commit();
        }finally {
            if(pm.currentTransaction().isActive()){
                pm.currentTransaction().rollback();
            }
            pm.close();
        }
    }   
    public static User getUser(User u) {
        if(pm.isClosed()) {
            pm = PMFactory.get().getPersistenceManager();
        }
        try {
            User usr = pm.getObjectById(ReemarkUser.class, u.getUserId());
        }finally {
            pm.close();
        }
        return user;
    }
}

Is it possible to get an Exception that indicates my objects being handled by another PersistentManager?