users@glassfish.java.net

Re: Database Disconnects on Redeploy

From: <glassfish_at_javadesktop.org>
Date: Thu, 21 Jun 2007 12:45:00 PDT

Here is the three Beans, I split them up by some '---' characters.

package com.stryker.cmf.accountrolebean;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityExistsException;
/**
 *
 * @author tony.mattas
 *
 */
@Stateless
public class AccountMgrBean implements AccountMgr {
        @EJB UsersFacadeLocal userfacade;
        @EJB GroupsFacadeLocal groupsfacade;
        
        public void adduser(String username, String password) throws EntityExistsException {
                Users iusername = userfacade.findById(username);
                /* Check for data sanity before hitting the DB adapter to prevent internal error */
                if (iusername != null) {
                        throw new EntityExistsException();
                }
                /* Now we'll create a new User object, since it currently is null */
                iusername = new Users();
                //TODO - Why does it get disconnected from database
                iusername.setUsername(username);
                try {
                        iusername.setPassword(hash(password));
                } catch (NoSuchAlgorithmException e){
                        //Print the stack trace since we are passing on a generic error.
                        e.printStackTrace();
                        //Pass a new exceptuion onto the caller.
                        throw new RuntimeException();
                }
                try {
                        userfacade.save(iusername);
                } catch (RuntimeException e) {
                        //Pass exception onto caller
                        throw e;
                }
        }
        /**
         * Generates a MD5 Hash
         * @param password Plain text password
         * @return Hashed Password
         * @throws NoSuchAlgorithmException
         */
        private String hash(String password) throws NoSuchAlgorithmException {
                String hashed = null;
                BigInteger hash;
                try {
                        MessageDigest md5 = MessageDigest.getInstance("MD5");
                        md5.update(password.getBytes());
                        hash = new BigInteger(1, md5.digest());
                        hashed = hash.toString(16);
                        if (hashed.length() < 32) {
                                hashed = 0 + hashed;
                        }
                } catch (NoSuchAlgorithmException e) {
                        throw e;
                }
                return hashed;
        }

        public void editgroup(String user, String group) throws RuntimeException {
                Users username = new Users();
                username = userfacade.findById(user);
                Groups groupname = new Groups(username.getUsername(), username, group);

                try {
                        groupsfacade.save(groupname);
                } catch (RuntimeException e) {
                        e.printStackTrace();
                        throw e;
                }
        }
}
        

------------------------------------------------------------------------------------------


package com.stryker.cmf.accountrolebean;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "users", schema = "public", uniqueConstraints = {})
public class Users implements java.io.Serializable {

        // Fields

        /**
         *
         */
        private static final long serialVersionUID = -820976001690568032L;

        private String username;

        private String password;

        private Set<Groups> groupses = new HashSet<Groups>(0);

        // Constructors

        /** default constructor */
        public Users() {
        }

        /** minimal constructor */
        public Users(String username, String password) {
                this.username = username;
                this.password = password;
        }

        /** full constructor */
        public Users(String username, String password, Set<Groups> groupses) {
                this.username = username;
                this.password = password;
                this.groupses = groupses;
        }

        // Property accessors
        @Id
        @Column(name = "username", unique = true, nullable = false, insertable = true, updatable = true, length = 50)
        public String getUsername() {
                return this.username;
        }

        public void setUsername(String username) {
                this.username = username;
        }

        @Column(name = "password", unique = false, nullable = false, insertable = true, updatable = true, length = 50)
        public String getPassword() {
                return this.password;
        }

        public void setPassword(String password) {
                this.password = password;
        }

        @OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "users")
        public Set<Groups> getGroupses() {
                return this.groupses;
        }

        public void setGroupses(Set<Groups> groupses) {
                this.groupses = groupses;
        }

}
---------------------------------------------------------------------------------------------------


package com.stryker.cmf.accountrolebean;

import java.util.List;
import java.util.logging.Level;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Facade for entity Users.
 *
 * @see com.stryker.cmf.accountrolebean.Users
 */
@Stateless
public class UsersFacade implements UsersFacadeLocal {
        // property constants
        public static final String PASSWORD = "password";

        @PersistenceContext
        private EntityManager entityManager;

        public void save(Users transientInstance) {
                EntityManagerHelper.log("saving Users instance", Level.INFO, null);
                try {
                        entityManager.persist(transientInstance);
                        EntityManagerHelper.log("save successful", Level.INFO, null);
                } catch (RuntimeException re) {
                        EntityManagerHelper.log("save failed", Level.SEVERE, re);
                        throw re;
                }
        }

        public void delete(Users persistentInstance) {
                EntityManagerHelper.log("deleting Users instance", Level.INFO, null);
                try {
                        entityManager.remove(persistentInstance);
                        EntityManagerHelper.log("delete successful", Level.INFO, null);
                } catch (RuntimeException re) {
                        EntityManagerHelper.log("delete failed", Level.SEVERE, re);
                        throw re;
                }
        }

        public Users update(Users detachedInstance) {
                EntityManagerHelper.log("updating Users instance", Level.INFO, null);
                try {
                        Users result = entityManager.merge(detachedInstance);
                        EntityManagerHelper.log("update successful", Level.INFO, null);
                        return result;
                } catch (RuntimeException re) {
                        EntityManagerHelper.log("update failed", Level.SEVERE, re);
                        throw re;
                }
        }

        public Users findById(String id) {
                EntityManagerHelper.log("finding Users instance with id: " + id,
                                Level.INFO, null);
                try {
                        Users instance = entityManager.find(Users.class, id);
                        return instance;
                } catch (RuntimeException re) {
                        EntityManagerHelper.log("find failed", Level.SEVERE, re);
                        throw re;
                }
        }

        @SuppressWarnings("unchecked")
        public List<Users> findByProperty(String propertyName, Object value) {
                EntityManagerHelper.log("finding Users instance with property: "
                                + propertyName + ", value: " + value, Level.INFO, null);
                try {
                        String queryString = "select model from Users model where model."
                                        + propertyName + "= :propertyValue";
                        return entityManager.createQuery(queryString).setParameter(
                                        "propertyValue", value).getResultList();
                } catch (RuntimeException re) {
                        EntityManagerHelper.log("find by property name failed",
                                        Level.SEVERE, re);
                        throw re;
                }
        }

        public List<Users> findByPassword(Object password) {
                return findByProperty(PASSWORD, password);
        }

        @SuppressWarnings("unchecked")
        public List<Users> findAll() {
                EntityManagerHelper
                                .log("finding all Users instances", Level.INFO, null);
                try {
                        String queryString = "select model from Users model";
                        return entityManager.createQuery(queryString).getResultList();
                } catch (RuntimeException re) {
                        EntityManagerHelper.log("find all failed", Level.SEVERE, re);
                        throw re;
                }
        }
}
[Message sent by forum member 'amattas' (amattas)]

http://forums.java.net/jive/thread.jspa?messageID=223398