<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chithra on Medium]]></title>
        <description><![CDATA[Stories by Chithra on Medium]]></description>
        <link>https://medium.com/@chithram1990?source=rss-f0c602cdb39f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xhwN6VErYbr1JHZ3GVlxww.png</url>
            <title>Stories by Chithra on Medium</title>
            <link>https://medium.com/@chithram1990?source=rss-f0c602cdb39f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 19:30:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@chithram1990/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Dramatic Life of a Jakarta Persistence Unit: What Happens When You Start and Stop]]></title>
            <link>https://medium.com/@chithram1990/the-dramatic-life-of-a-jakarta-persistence-unit-what-happens-when-you-start-and-stop-b1d6e8d0c916?source=rss-f0c602cdb39f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b1d6e8d0c916</guid>
            <category><![CDATA[jpa]]></category>
            <dc:creator><![CDATA[Chithra]]></dc:creator>
            <pubDate>Wed, 07 Oct 2026 05:52:43 GMT</pubDate>
            <atom:updated>2026-10-07T05:52:43.093Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*Hn6M965rwhgUjqA0KGmKcQ.png" /></figure><p>If the Jakarta Persistence Unit had a personality, it would be a high-maintenance celebrity demanding attention when it arrives, insisting on being reused, and throwing a fit if you forget to say goodbye. At the center of this drama is the EntityManagerFactory, the star performer that makes your database interactions possible. Understanding its lifecycle isn’t just good manners - it’s essential for keeping your application fast, stable, and leak-free.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pLLeZiVFI3gYTQVoDiGy2w.jpeg" /></figure><p>When your application starts, Jakarta Persistence doesn’t just quietly initialise. It bursts onto the scene, scanning your persistence.xml like it’s reading a script for its next big role. The creation of the EntityManagerFactory is a full production: metadata is parsed, entity classes are inspected, connection pools are filled, and query caches are prepped. This isn’t a lightweight operation; it’s more like assembling a Broadway set than flipping a light switch. That’s why you should never create an EntityManagerFactory casually or repeatedly. It’s expensive, and it expects to be treated like royalty.</p><p>Once the EntityManagerFactory is created, it wants to be reused. Think of it like Beyoncé — you don’t hire her for every birthday party. You book her once, and you make the most of it. In code, this means storing the factory in a static field or a singleton, and using it to produce EntityManager instances as needed. For example:</p><pre>   public class PersistenceManager {<br>        private static final EntityManagerFactory emf =<br>                    Persistence.createEntityManagerFactory(&quot;my-persistence-unit&quot;);<br>        public static EntityManager getEntityManager() {<br>            return emf.createEntityManager();<br>        }<br>    }</pre><p>This approach ensures that the heavy lifting is done only once, and your application can glide smoothly through its transactions without repeatedly rebuilding the stage.</p><p>But every show must end, and when your application shuts down, the EntityManagerFactory expects a graceful exit. You don’t just pull the plug- you close the curtain, dim the lights, and let the crew clean up. Calling emf.close() is your way of saying “thank you for your service.” Behind the scenes, this releases database connections, clears caches, shuts down background threads, and frees up memory. Forgetting to do this is like leaving the venue open overnight. Eventually the rats move in, and your app starts leaking memory and misbehaving.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m0XUycfdcJXgYE3Nwail4g.jpeg" /></figure><p>In test environments, the stakes are even higher. If you don’t close the factory between tests, you risk leftover state contaminating your next test run. It’s like trying to rehearse a new scene while the props from the last one are still on stage. Use teardown hooks like @AfterAll to make sure the factory gets the exit it deserves:</p><pre>    @AfterAll<br>    static void tearDown() {<br>        emf.close();<br>    }</pre><p>Frameworks like Spring Boot and Jakarta EE act like professional stage managers. They handle the lifecycle for you, making sure the EntityManagerFactory is created once, reused appropriately, and closed when the show’s over. In Spring Boot, you can simply inject the EntityManager using @PersistenceContext, and the framework takes care of the rest. Jakarta EE does the same, letting you focus on your business logic while it handles the backstage drama.</p><p>So what’s the moral of the story? The EntityManagerFactory is your persistence unit’s diva. Treat it with respect. Create it once, reuse it like a pro, and close it with flair. Whether you’re building a microservice, a monolith, or just trying to survive integration tests, understanding this lifecycle will help you avoid performance meltdowns, memory leaks, and transactional chaos. And if you ever forget to close it — just remember, even divas need their beauty sleep.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1d6e8d0c916" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>