{"id":2576,"date":"2017-11-21T09:58:29","date_gmt":"2017-11-21T09:58:29","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2576"},"modified":"2017-12-12T17:40:20","modified_gmt":"2017-12-12T17:40:20","slug":"persistence-vs-application-context","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/persistence-vs-application-context\/","title":{"rendered":"persistence vs application context"},"content":{"rendered":"<p><strong>Difference between configuring data source in persistence.xml and spring (or) application-context.xml files<\/strong><\/p>\n<p><strong>persistence-context.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;persistence-unit name=&quot;LocalDB&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;\r\n    &lt;class&gt;domain.User&lt;\/class&gt;\r\n    &lt;exclude-unlisted-classes&gt;true&lt;\/exclude-unlisted-classes&gt;\r\n    &lt;properties&gt;\r\n        &lt;property name=&quot;hibernate.connection.driver_class&quot; value=&quot;org.hsqldb.jdbcDriver&quot;\/&gt;\r\n        &lt;property name=&quot;hibernate.connection.url&quot; value=&quot;jdbc:hsqldb:hsql:\/\/localhost&quot;\/&gt;\r\n        &lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;create&quot;\/&gt;\r\n        &lt;property name=&quot;hibernate.c3p0.min_size&quot; value=&quot;5&quot;\/&gt;\r\n        ....\r\n        &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.HSQLDialect&quot;\/&gt;\r\n    &lt;\/properties&gt;\r\n&lt;\/persistence-unit&gt;\r\n<\/pre>\n<p><strong>application-context.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;bean id=&quot;domainEntityManagerFactory&quot; class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;\r\n    &lt;property name=&quot;persistenceUnitName&quot; value=&quot;JiraManager&quot;\/&gt;\r\n    &lt;property name=&quot;dataSource&quot; ref=&quot;domainDataSource&quot;\/&gt;\r\n    &lt;property name=&quot;jpaVendorAdapter&quot;&gt;\r\n        &lt;bean class=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot;&gt;\r\n            &lt;property name=&quot;generateDdl&quot; value=&quot;false&quot;\/&gt;\r\n            &lt;property name=&quot;showSql&quot; value=&quot;false&quot;\/&gt;\r\n            &lt;property name=&quot;databasePlatform&quot; value=&quot;${hibernate.dialect}&quot;\/&gt;\r\n        &lt;\/bean&gt;\r\n    &lt;\/property&gt;\r\n&lt;\/bean&gt;\r\n\r\n&lt;bean id=&quot;domainDataSource&quot; class=&quot;com.mchange.v2.c3p0.ComboPooledDataSource&quot; destroy-method=&quot;close&quot;&gt;\r\n    &lt;property name=&quot;driverClass&quot; value=&quot;${db.driver}&quot; \/&gt;\r\n    &lt;property name=&quot;jdbcUrl&quot; value=&quot;${datasource.url}&quot; \/&gt;\r\n    &lt;property name=&quot;user&quot; value=&quot;${datasource.username}&quot; \/&gt;\r\n    &lt;property name=&quot;password&quot; value=&quot;${datasource.password}&quot; \/&gt;\r\n    &lt;property name=&quot;initialPoolSize&quot; value=&quot;5&quot;\/&gt;\r\n    &lt;property name=&quot;minPoolSize&quot; value=&quot;5&quot;\/&gt;\r\n    .....\r\n&lt;\/bean&gt;\r\n<\/pre>\n<p>If you are using <strong>persistence.xml<\/strong> you&#8217;re creating your own connection pool and do not profit from the existing connection pool in the container. Thus even if you configured your container to, say, a max of 20 simultaneous connections to the database, you can&#8217;t guarantee this max as this new connection pool is not restrained by your configuration. Also, you don&#8217;t profit from any monitoring tools your container provides you.<\/p>\n<p>If you are using <strong>spring (or) application.xml<\/strong><br \/>\nIf you&#8217;re also creating your own connection pool, with the same disadvantages as above. However, you can isolate the definition of this spring bean and only use it in test runs.Your best bet is to look up the container&#8217;s connection pool via JNDI. Then you are sure to respect the data source configurations from the container.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Difference between configuring data source in persistence.xml and spring (or) application-context.xml files persistence-context.xml &lt;persistence-unit name=&quot;LocalDB&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt; &lt;class&gt;domain.User&lt;\/class&gt; &lt;exclude-unlisted-classes&gt;true&lt;\/exclude-unlisted-classes&gt; &lt;properties&gt; &lt;property name=&quot;hibernate.connection.driver_class&quot; value=&quot;org.hsqldb.jdbcDriver&quot;\/&gt; &lt;property name=&quot;hibernate.connection.url&quot; value=&quot;jdbc:hsqldb:hsql:\/\/localhost&quot;\/&gt; &lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;create&quot;\/&gt; &lt;property name=&quot;hibernate.c3p0.min_size&quot; value=&quot;5&quot;\/&gt; &#8230;. &lt;property name=&quot;hibernate.dialect&quot; value=&quot;org.hibernate.dialect.HSQLDialect&quot;\/&gt; &lt;\/properties&gt; &lt;\/persistence-unit&gt; application-context.xml &lt;bean id=&quot;domainEntityManagerFactory&quot; class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt; &lt;property name=&quot;persistenceUnitName&quot; value=&quot;JiraManager&quot;\/&gt; &lt;property name=&quot;dataSource&quot; ref=&quot;domainDataSource&quot;\/&gt; &lt;property name=&quot;jpaVendorAdapter&quot;&gt; &lt;bean class=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot;&gt; &lt;property name=&quot;generateDdl&quot; value=&quot;false&quot;\/&gt; &lt;property&hellip; <a href=\"https:\/\/codethataint.com\/blog\/persistence-vs-application-context\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[236],"tags":[],"class_list":["post-2576","post","type-post","status-publish","format-standard","hentry","category-interview-questions-jpa"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2576"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2576\/revisions"}],"predecessor-version":[{"id":2586,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2576\/revisions\/2586"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}