Different users for DB connection in EntityManager


EJB programming & troubleshooting: Different users for DB connection in EntityManager

  1. Different users for DB connection in EntityManager (1 messages)

    Hi all, I’m trying to develop an application with EJB 3.0, JPA. Somebody knows the way to define the user and password used to connect to the Database every time the Entity Manager is declared? I need to use the login information from my application (user and password) to connect to the DB. I’m using a code like this: @Stateless public class CustomerTaskImpl implements CustomerTask { @PersistenceContext(name="OrderDB", properties={ @PersistenceProperty(name="openjpa.ConnectionUserName", value="xxxxxx"), @PersistenceProperty(name="openjpa.ConnectionPassword", value="yyyyyy") } ) EntityManager em; public Customer findCustomer(int customerId) throws CustomerDoesNotExist { Customer customer = (Customer) em.find(Customer.class, customerId); if (customer == null) { throw new CustomerDoesNotExist("Customer does not exist"); } return customer; } } but the value for user and password must be a constant, and don’t work in every JDBC driver. I would appreciate very much if somebody can tell me what I am trying to do is possible or not.
  2. Entity manager is heavy Object and initializing every time is not recommend. If you have limited number of users, I am suggesting is to have different entity manager for different users. Then switch the entity manager for your user when user login to the system, and keep entity manager in your session. Not very good but at least is a way. Ali Kianzadeh