deleteDocument doesn't work

View: New views
1 Messages — Rating Filter:   Alert me  

deleteDocument doesn't work

by giancarlo :: Rate this Message:

Reply (Restricted by the Administrator) | Reply to Author | View Threaded | Show Only this Message

hallo,
I have a problem. I have created 2 Java class for save and delete XMLDocument, but the delete Class is not working:

class DeleteDocumentCommand extends AbstractCommand {

        private String name;
        private XmlUpdateContext updateContext;
        private XmlTransaction txn;

        public DeleteDocumentCommand(DbEnv dbEnv,String name) {
                super(dbEnv);
                this.name = name;
        }

        public void close() throws CommandException {
                try {
                        if(txn!=null){
                                txn.commit();
                                txn.delete();
                        }
                        if(updateContext!=null){
                                updateContext.delete();
                        }
                } catch (XmlException e) {
                        e.printStackTrace();
                        throw new CloseCommandException(e);
                }
        }

        public Object execute() throws CommandException {
                try{
                        openedContainer.deleteDocument(txn,name, updateContext);
                        return null;
                        }catch(Exception e){
                                e.printStackTrace();
                                throw new ExecuteCommandException(e);
                }
        }

        public void open() throws CommandException {
                theMgr = dbEnv.getManager();
                XmlContainerConfig config = new XmlContainerConfig();
                config.setTransactional(true);
                try {
                        openedContainer = theMgr.openContainer(XMLDBConstant.theContainer, config);
                        updateContext = theMgr.createUpdateContext();
                        txn = theMgr.createTransaction();
                } catch (XmlException e) {
                        throw new OpenCommandException(e);
                }
        }

}


class SaveDocumentCommand extends AbstractCommand {
       
        private String name;
        private String content;
        private XmlUpdateContext updateContext;
        private XmlTransaction txn;

        public SaveDocumentCommand(DbEnv dbEnv,String name,String content) {
                super(dbEnv);
                this.name = name;
                this.content = content;
                // TODO Auto-generated constructor stub
        }

        public void close() throws CommandException {
                try {
                        if(txn!=null){
                                txn.commit();
                                txn.delete();
                        }
                        if(updateContext!=null){
                                updateContext.delete();
                        }
                } catch (XmlException e) {
                        throw new CloseCommandException(e);
                }
        }

        public Object execute() throws CommandException {
                try{
                        XmlDocument xmlDoc = theMgr.createDocument();
                        xmlDoc.setContent(content);
                        xmlDoc.setName(name);
                        Date theDate = new Date();
                        xmlDoc.setMetaData(XMLDBConstant.uri, XMLDBConstant.name, new XmlValue(theDate.toString()));
                        openedContainer.putDocument(txn, xmlDoc, updateContext);
                        return null;
                }catch(Exception e){
                        throw new ExecuteCommandException(e);
                }
               
        }

        public void open() throws CommandException {
                theMgr = dbEnv.getManager();
                XmlContainerConfig config = new XmlContainerConfig();
                config.setTransactional(true);
                try {
                        openedContainer = theMgr.openContainer(XMLDBConstant.theContainer, config);
                        updateContext = theMgr.createUpdateContext();
                        txn = theMgr.createTransaction();
                } catch (XmlException e) {
                        throw new OpenCommandException(e);
                }
               
                       
        }


public class DbEnv
{
       

        private Environment dbEnv_ = null;
        private XmlManager mgr_ = null;
        private File path2DbEnv_ = null;
       
        public static boolean exist = false;

        public DbEnv() throws Exception  {
                File path2DbEnv = new File(XMLDBConstant.path2DbEnvString);
                if (! path2DbEnv.isDirectory()) {
                        throw new Exception(path2DbEnv.getPath() +
                        " does not exist or is not a directory.");
                }

                EnvironmentConfig config = new EnvironmentConfig();
                config.setCacheSize(50 * 1024 * 1024); // 50MB
                config.setAllowCreate(true);
                 config.setInitializeCache(true);
                config.setTransactional(true);
                config.setInitializeLocking(true);
                config.setInitializeLogging(true);
                config.setErrorStream(System.err);
                dbEnv_ = new Environment(path2DbEnv, config);

                path2DbEnv_ = path2DbEnv;
                mgr_ = new XmlManager(dbEnv_, null);
                if(!exist){
                        if(mgr_.existsContainer(XMLDBConstant.theContainer)==0){
                                System.out.println("Sto creando il DB");
                                mgr_.createContainer(XMLDBConstant.theContainer);
                        }
                        exist=true;
                }
        }
       
       
        public File getDbEnvPath() { return path2DbEnv_; }

        public Environment getEnvironment() { return dbEnv_; }

        public XmlManager getManager() { return mgr_; }

        public void cleanup() throws DatabaseException
        {
                if (mgr_ != null) {
                        mgr_.delete();
                        mgr_ = null;
                }
                if (dbEnv_ != null) {
                        dbEnv_.close();
                        dbEnv_ = null;
                }

        }


I'm using my class:

Command command = new DeleteDocumentCommand (new DbEnv(),"name.xml");
command.open();
command.execute();
command.close();

The method openedContainer.deleteDocument(txn,name, updateContext)  seems blocked. I waited for, but it does not go ahead;

Can you help me?
Thanks
giancarlo