现在的位置: 首页 > 综合 > 正文

EJB 连接DB util — EntityManagerUtil

2012年10月03日 ⁄ 综合 ⁄ 共 2871字 ⁄ 字号 评论关闭

====1 persistence.xml配置文件==============================

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
 xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 <persistence-unit name="CICI_PERSISTENCE_UNIT" transaction-type="RESOURCE_LOCAL">

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

<!-- the Entity model-->

<class>com.cici.crm.migration.model.crm.File</class>

<properties>

       <property name="eclipselink.logging.level" value="OFF"/>

                     <property name="eclipselink.target-database" value="Oracle"/>

                    <property name ="eclipselink.jdbc.url"    value="jdbc:oracle:thin:@localhost:1521:OTADB"/>

                     <property name="eclipselink.jdbc.driver" value="oracle.jdbc.driver.OracleDriver"/>

                     <property name="eclipselink.jdbc.user" value="cici"/>

                     <property name="eclipselink.jdbc.password" value="cici"/>

                     <property name="eclipselink.ddl-generation" value="none"/>

</properties>

</persistence-unit>

</persistence>

 

====EntityManagerUtil.java================================

import java.util.HashMap;

import java.util.Map;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

import com.cici.otang.migration.config.Configuration;

/*To maintain the life cycle of all the entity manager*/

 

public class EntityManagerUtil{

  private EntityManagerFactory cicifactory = null;

      private static final String CICI_PERSISTENCE_UNIT =

       "CICI_PERSISTENCE_UNIT";

       /*Singelton :lazy loading*/

       private static EntityManagerUtil instance = null;

       private EntityManagerUtil(){

        Map<String,String>properties = new HashMap<String,String>();
        properties.put ("eclipselink.jdbc.url",Configuration.getInstance ().getValue (Configuration.OTAM_DATABASE_URL));
        properties.put("eclipselink.jdbc.user", Configuration.getInstance ().getValue (Configuration.OTAM_DATABASE_USERNAME));
        properties.put("eclipselink.jdbc.password",Configuration.getInstance().getValue(Configuration.OTAM_DATABASE_PASSWORD));
        rcaFactory = Persistence.createEntityManagerFactory(RCA_PERSISTENCE_UNIT, properties);

}

/*Singleton :lazy loading :initail the

To ensure that the whole APP share one EntityManagerUtil instance

*/

 

public synchronized static EntityManagerUtil getInstance(){

              if(instance==null){

               instance = new EntityManagerUtil();

              }

              return instance;

}

      public EntityManager getCICIEntityManager(){

               return cicifactory.createEntityManager(); 

     }

   public  void releaseEntityManager(final EntityManager entityManager){

                           entityManager.close();

   }

    public void destory(){

       ciciFactory.close();

       instance = null;

    }

}

}

 

抱歉!评论已关闭.