Your SlideShare is downloading. ×
0
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Secure REST with JAX-RS
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Secure REST with JAX-RS

39,663

Published on

Build Secure RESTful web services

Build Secure RESTful web services

Published in: Technology
0 Comments
53 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
39,663
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
1,283
Comments
0
Likes
53
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Representational State Transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web. The term was introduced in the doctoral dissertation of Roy Fielding in 2000, and has since come into widespread use in the networking community. An important concept in REST is the existence of resources, each of which can be referred to using a global identifier, that is, a URI. In order to manipulate these resources, components of the network, clients and servers, communicate using a standardized interface such as HTTP and exchange representations of these resources.
  • What Are RESTful Web Services?
    Representational State Transfer (REST) is a software application architecture modeled after the way data is represented, accessed, and modified on the web. In the REST architecture, data and functionality are considered resources, and these resources are accessed using Uniform Resource Identifiers (URIs), typically links on the web. The resources are acted upon by using a set of simple, well-defined operations. The REST architecture is designed to use a stateless communication protocol, typically HTTP. In the REST architecture, clients and servers exchange representations of resources using a standardized interface and protocol. These principles encourages REST applications to be simple, lightweight, and have high performance.
    RESTful web services are web applications built upon the REST architecture. They:
    expose resources (data and functionality) through web URIs
    use the four main HTTP methods to create, retrieve, update, and delete resources
    RESTful web services typically map the four main HTTP methods to the so-called CRUD actions: create, retrieve, update, and delete. The following table shows a mapping of HTTP methods to these CRUD actions.
  • REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used (which often differs quite a bit from what many people actually do). The promise is that if you adhere to REST principles while designing your application, you will end up with a system that exploits the Web’s architecture to your benefit. In summary, the five key principles are:
    * Give every “thing” an ID
    * Link things together
    * Use standard methods
    * Resources with multiple representations
    * Communicate statelessly
    Let’s take a closer look at each of these principles.
  • Everything that should be identifiable should obviously get an ID — on the Web, there is a unified concept for IDs: The URI. URIs make up a global namespace, and using URIs to identify your key resources means they get a unique, global ID.
    an Order resource might be composed of order items, an address and many other aspects that you might not want to expose as individually identifiable resources. Taking the idea of identifying everything that is worth being identified further leads to the creation of resources that you usually don’t see in a typical application design: A process or process step, a sale, a negotiation, a request for a quote — these are all examples of “things” that merit identification.
    Use URIs to identify everything that merits being identifiable, specifically, all of the “high-level” resources that your application provides, whether they represent individual items, collections of items, virtual and physical objects, or computation results.
    examples of URIs you might come up with:
    http://example.com/customers/1234
    http://example.com/orders/2007/10/776654
    http://example.com/orders/2007/11
    http://example.com/products?color=green
  • Everything that should be identifiable should obviously get an ID — on the Web, there is a unified concept for IDs: The URI. URIs make up a global namespace, and using URIs to identify your key resources means they get a unique, global ID.
    an Order resource might be composed of order items, an address and many other aspects that you might not want to expose as individually identifiable resources. Taking the idea of identifying everything that is worth being identified further leads to the creation of resources that you usually don’t see in a typical application design: A process or process step, a sale, a negotiation, a request for a quote — these are all examples of “things” that merit identification.
    Use URIs to identify everything that merits being identifiable, specifically, all of the “high-level” resources that your application provides, whether they represent individual items, collections of items, virtual and physical objects, or computation results.
    examples of URIs you might come up with:
    http://example.com/customers/1234
    http://example.com/orders/2007/10/776654
    http://example.com/orders/2007/11
    http://example.com/products?color=green
  • REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used (which often differs quite a bit from what many people actually do). The promise is that if you adhere to REST principles while designing your application, you will end up with a system that exploits the Web’s architecture to your benefit. In summary, the five key principles are:
    * Give every “thing” an ID
    * Link things together
    * Use standard methods
    * Resources with multiple representations
    * Communicate statelessly
    Let’s take a closer look at each of these principles.
  • you can rely on being able to retrieve a representation using GET. Because GET’s semantics are defined in the specification, you can be sure that you have no obligations when you call it — this is why the method is called “safe”. GET supports very efficient and sophisticated caching, so in many cases, you don’t even have to send a request to the server. You can also be sure that a GET is idempotent — if you issue a GET request and don’t get a result, you might not know whether your request never reached its destination or the response got lost on its way back to you. The idempotence guarantee means you can simply issue the request again.
  • The uniform interface also enables every component that understands the HTTP application protocol to interact with your application. Examples of components that benefit from this are generic clients such as curl and wget, proxies, caches, HTTP servers, gateways, even Google/Yahoo!/MSN, and many more.
    To summarize: For clients to be able to interact with your resources, they should implement the default application protocol (HTTP) correctly, i.e. make use of the standard methods GET, PUT, POST, DELETE.
  • Idempotence is also guaranteed for PUT (which basically means “update this resource with this data, or create it at this URI if it’s not there already”) and for DELETE (which you can simply try again and again until you get a result — deleting something that’s not there is not a problem). POST, which usually means “create a new resource”, can also be used to invoke arbitrary processing and thus is neither safe nor idempotent.
  • REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used (which often differs quite a bit from what many people actually do). The promise is that if you adhere to REST principles while designing your application, you will end up with a system that exploits the Web’s architecture to your benefit. In summary, the five key principles are:
    * Give every “thing” an ID
    * Link things together
    * Use standard methods
    * Resources with multiple representations
    * Communicate statelessly
    Let’s take a closer look at each of these principles.
  • If you look at the product and customer links in this document, you can easily imagine how an application that has retrieved it can “follow” the links to retrieve more information. the links can point to resources that are provided by a different application, a different server, or even a different company on another continent — because the naming scheme is a global standard, all of the resources that make up the Web can be linked to each other.
    the service provides a set of links to the client enabling the client to move the application from one state to the next by following a link. links are an extremely useful way to make an application dynamic.
    Use links to refer to identifiable things (resources) wherever possible. Hyperlinking is what makes the Web the Web
    Addressability and connectedness. Resources have their representations, but providing representations of resources would be useless if you could not address them. In REST, every resource must have at least one address, that is, one URI. To address the resource, you simply specify the URI. This concept is called "addressability". By publishing your web application, you introduce many different URIs that are connected to each other. Because of that connectedness, the only URI you need to give to your clients is one URI called the "bootstrap URI".
  • REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used (which often differs quite a bit from what many people actually do). The promise is that if you adhere to REST principles while designing your application, you will end up with a system that exploits the Web’s architecture to your benefit. In summary, the five key principles are:
    * Give every “thing” an ID
    * Link things together
    * Use standard methods
    * Resources with multiple representations
    * Communicate statelessly
    Let’s take a closer look at each of these principles.
  • Using HTTP content negotiation, a client can ask for a representation in a particular format:
    GET /customers/1234 HTTP/1.1
    Host: example.com
    Accept: application/vnd.mycompany.customer+xml
    if a client “knows” both the HTTP application protocol and a set of data formats, it can interact with any RESTful HTTP application in the world in a very meaningful way.
    Provide multiple representations of resources for different needs.
    a server that can consume data in specific formats does not care about the particular type of client, provided it follows the application protocol.
    significant benefit of having multiple representations of a resource in practice: If you provide both an HTML and an XML representation of your resources, they are consumable not only by your application, but also by every standard Web browser —information in your application becomes available to everyone who knows how to use the Web.
    You can turn your application’s Web UI into its Web API — everything that can be done via the UI should also be doable via the API. useful way to get a better Web interface for both humans and other applications.
  • REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used (which often differs quite a bit from what many people actually do). The promise is that if you adhere to REST principles while designing your application, you will end up with a system that exploits the Web’s architecture to your benefit. In summary, the five key principles are:
    * Give every “thing” an ID
    * Link things together
    * Use standard methods
    * Resources with multiple representations
    * Communicate statelessly
    Let’s take a closer look at each of these principles.
  • REST mandates that state be either turned into resource state, or kept on the client. In other words, a server should not have to retain some sort of communication state for any of the clients it communicates with beyond a single request. The most obvious reason for this is scalability — the number of clients interacting would seriously impact the server’s footprint if it had to keep client state. (Note that this usually requires some re-design — you can’t simply stick a URI to some session state and call it RESTful.)
    But there are other aspects that might be much more important: The statelessness constraint isolates the client against changes on the server as it is not dependent on talking to the same server in two consecutive requests. A client could receive a document containing links from the server, , the server could be shut down, — and if the client follows one of the links it has received from the server, it won’t notice
    Statelessness. Statelessness means that a web application is not responsible for keeping any information about the state of its clients. REST does not encompass the concept of HTTP sessions. The client is responsible for tracking its own actions (if it needs to). The service maintains its resources and provides a uniform interface to the clients.
  • Use an annotation to mark the methods that you want the runtime to to call to service the http request. Can either put http method in the annotation or in the method
  • From Göteborg
    Callista
  • The @ConsumeMime annotation is used to specify which MIME types a resource class, method, or
    MessageBodyReader can accept. If @ConsumeMime is applied at the class level, all the response
    methods accept the specified MIME types by default. If @ConsumeMime is applied at the method
    level, it overrides any @ConsumeMime annotations applied at the class level.@ProduceMime annotation is used to specify the MIME types a resource or MessageBodyWriter can produce and send back to the client. If @ProduceMime is applied at the class level, all the methods in a resource can produce the specified MIME types by default. If it is applied at the method level, it overrides any @ProduceMime annotations applied at the class level.
  • Multiple parameters may be extracted
    from the URI using the PathParam or QueryParam annotations
    Query parameters are extracted from the request URI query parameters, and are specified by
    using the javax.ws.rs.QueryParam annotation in the method parameter arguments.
    URI path parameters are extracted from the request URI, and the parameter names correspond
    to the URI path template variable names specified in the @Path class-level annotation. URI
    parameters are specified using the javax.ws.rs.PathParam annotation in the method
    parameter arguments.
  • Query parameters are extracted from the request URI query parameters, and are specified by using the QueryParam annotation in the method parameter arguments.
    URI parameters are extracted from the request URI, and the parameter names correspond to the URI Template variable names specified in the @Path class-level annotation. URI parameters are specified using the @PathParam annotation in the method parameter arguments.
  • @QueryParam and @PathParam can only be used on the following types:
    ■ all primitive types except char
    ■ all wrapper classes of primitive types except Character
    ■ String
    ■ any class with the static method valueOf(String)
    ■ any class with a constructor that takes a single String as a parameter
    ■ List<T>, where T matches the already listed criteria
  • The HTTP specification defines what HTTP response codes should be on a successful request. For example, GET should return 200, OK and PUT should return 201, CREATED. You can expect JAX-RS to return the same default response codes.
    Sometimes, however, you need to specify your own response codes, or simply to add specific headers or cookies to your HTTP response. JAX-RS provides a Response class for this.
  • The uniform interface also enables every component that understands the HTTP application protocol to interact with your application. Examples of components that benefit from this are generic clients such as curl and wget, proxies, caches, HTTP servers, gateways, even Google/Yahoo!/MSN, and many more.
    To summarize: For clients to be able to interact with your resources, they should implement the default application protocol (HTTP) correctly, i.e. make use of the standard methods GET, PUT, POST, DELETE.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • REST is a set of principles that define how Web standards, such as HTTP and URIs, are supposed to be used (which often differs quite a bit from what many people actually do). The promise is that if you adhere to REST principles while designing your application, you will end up with a system that exploits the Web’s architecture to your benefit. In summary, the five key principles are:
    * Give every “thing” an ID
    * Link things together
    * Use standard methods
    * Resources with multiple representations
    * Communicate statelessly
    Let’s take a closer look at each of these principles.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • To obtain an container managed EntityManager instance, inject the entity manager into the application component:
    @PersistenceContext
    EntityManager em;
    you don't need any cm lifecycle methods
    With a container-managed entity manager, an EntityManager instance's persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
    JTA transactions usually involve calls across application components. To complete a JTA transaction, these components usually need access to a single persistence context. This occurs when an EntityManager is injected into the application components via the javax.persistence.PersistenceContext annotation. The persistence context is automatically propagated with the current JTA transaction, and EntityManager references that are mapped to the same persistence unit provide access to the persistence context within that transaction. By automatically propagating the persistence context, application components don't need to pass references to EntityManager instances to each other in order to make changes within a single transaction. The Java EE container manages the lifecycle of container-managed entity managers.
  • ×