了解JPA中的Persistence.xml [英] Understanding Persistence.xml in JPA

查看:144
本文介绍了了解JPA中的Persistence.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解以下内容:

I am trying to understand the following things:

当我创建一个EJB项目并将其部署到Glassfish时,我将在管理员中设置JDBC资源/连接池Glassfish的中心,还是在persistence.xml中添加用户名,密码等的所有不同属性?我不明白有一点。

When I make an EJB project and deploys it to Glassfish do I set up JDBC resources/connection pools at the administrator center of Glassfish or do I add all the different properites for username, password etc in the persistence.xml? I don't understand one bit of that.

我不明白为什么我们同时拥有JDBC资源和JDBC连接池。它们有什么区别呢?有人可以向我解释这些东西或/或提供一些关于persistence.xml文件及其周围部分的好的链接?

I do not understand why we have both JDBC resource and JDBC Connection pool either. What is it and what is the difference between them? Could somebody explain me these things or/and provide some good link about the persistence.xml file and the part around it?

推荐答案

最好定义JDBC资源,而不是将信息放在persistence.xml中。这样你就可以利用连接池了。您将定义在persistence.xml中为JDBC资源提供的JNDI名称。

It's better to define a JDBC resource rather than putting the information in the persistence.xml. In this way you are going to take advantage of connection pooling. You are going to define the JNDI name you provided for the JDBC resource in the persistence.xml.


它是什么和有什么区别在他们之间

What is it and what is the difference between them

下面我粘贴了Glassfish 3.x帮助的一些部分。一探究竟。这是非常有用的

Below I pasted some parts of the Glassfish 3.x help. Check it out. It's really helpful

JDBC连接池

JDBC连接池包含一个组在连接池注册时创建的JDBC连接

A JDBC connection pool contains a group of JDBC connections that are created when the connection pool is registered

JDBC资源

Java数据库连接(JDBC)资源(数据源)为应用程序提供连接到数据库的方法。通常,管理员为部署在域中的应用程序访问的每个数据库创建一个JDBC资源;然而,可以为数据库创建多个JDBC资源。

A Java DataBase Connectivity (JDBC) resource (data source) provides applications with the means of connecting to a database. Typically, the administrator creates a JDBC resource for each database accessed by the applications deployed in a domain; however, more than one JDBC resource can be created for a database.

应用程序通过查找Java命名和目录中的数据源从连接池获取数据库连接接口(JNDI)API树,然后请求连接。与数据源关联的连接池提供与应用程序的连接。

Applications get a database connection from a connection pool by looking up a data source on the Java Naming and Directory Interface (JNDI) API tree and then requesting a connection. The connection pool associated with the data source provides the connection to the application.

将数据源(JDBC)资源视为某种类型数据的外观的工厂服务(连接池)。在这种情况下,它隐式地从池中获取连接并将其提供给您的应用程序。

Think of the data source(JDBC) resource as a factory for a facade of some type of data service(Connection Pool). In this case it implicitly gets a connection from the pool and provides it to your application.

一个示例persistence.xml:

An example persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
  <persistence-unit name="WebApplication2PU" transaction-type="JTA">
    <jta-data-source>jdbc/sample</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

这行是我给我的JDBC资源的JNDI名称:

This line is the JNDI name I gave to my JDBC resourse:

 <jta-data-source>jdbc/sample</jta-data-source>

您不需要在persistence.xml中定义与数据库连接相关的任何方式...只是资源的JNDI名称

You dont need to define anything related to the database connection in your persistence.xml this way...just the JNDI name of the resource

这篇关于了解JPA中的Persistence.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆