博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EJB 连接DB util -- EntityManagerUtil
阅读量:7098 次
发布时间:2019-06-28

本文共 2602 字,大约阅读时间需要 8 分钟。

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

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0"
 xmlns="" xmlns:xsi=""
 xsi:schemaLocation=" ">
 <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;

    }

}

}

 

转载于:https://www.cnblogs.com/cici-new/archive/2012/11/24/2786491.html

你可能感兴趣的文章
怎么才能在windows使用git命令
查看>>
Sigar应用
查看>>
从单体架构到微服务的演变之路
查看>>
Valgrind内存泄露检测工具使用初步
查看>>
PDF 补丁丁 0.5.0.2657 发布
查看>>
vue之axios使用
查看>>
VBA批量删除excel表高级版
查看>>
docker & nodejs & mongodb
查看>>
css 清除浮动
查看>>
Python_Selenium学习笔记(2)-浏览器操作方法
查看>>
excel自定义函数添加和使用方法
查看>>
C# 压缩组件介绍与入门
查看>>
结对学习心得感想及创意照
查看>>
sug
查看>>
windows 环境变量
查看>>
input checked取值
查看>>
快速幂取模(当数很大时,相乘long long也会超出的解决办法)
查看>>
EF+Code First+Database First+Model First,EF开发流程
查看>>
HttpWebRequest的常见错误使用TcpClient可避免
查看>>
报表技术
查看>>