Wednesday, May 28, 2014

Same tomcat context file serving multiple workspace

In development we encounter a common scenario of working on multiple branches @ a same time. For this we generally keep multiple copies of context file for each branch.
Ex: We have two branch, that has code in workspace_one and workspace_two, so for running code from each branch we need two context file like below
<Context docBase="F:\workspace\workspace_one\xyz\target\endpoint-1.0.0-SNAPSHOT" >
</Context>
<Context docBase="F:\workspace\workspace_two\xyz\target\endpoint-1.0.0-SNAPSHOT" >
</Context>
Similarly if we are working on n no of  branches then there will be  n no of context files.

Solution:
-DAPP_PATH="F:\workspace\workspace_one"  set this in VM arguments either from command line from where you are running tomcat or in eclipse configuration under VM arguments.
Modified the single copy of context file:
<Context docBase="${APP_PATH}\xyz\target\endpoint-1.0.0-SNAPSHOT" >
 </Context>
So each time you just need to make change in VM arguments before starting the tomcat only. No need to switch all context files.

Tomcat Resource Config

For multiple application sharing common resource, instead of defining resource in each context xml can be defined globally to share with all context in same tomcat container.
Pro/Cons: Change @ one place will reflect in all application context
Steps:
  • Define resource in <tomcat-home>/conf/server.xml
<GlobalNamingResources>
<!--
other resources
-->
<Resource name="jdbc/hcentivePHIXDS" global="jdbc/hcentivePHIXDS" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="25" maxWait="10000"
username="username" password="password" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:orcl" factory="org.apache.commons.dbcp.BasicDataSourceFactory" />

</GlobalNamingResources>
  • Define resource link in <tomcat-home>/conf/context.xml
<context>
<!--
other elements
-->
<ResourceLink type="javax.sql.DataSource"
name="jdbc/hcentivePHIXDS"
global="jdbc/hcentivePHIXDS"/>
</context>

Hibernate General Exceptions

Common exceptions that we encountered in hibernate:

  • Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing:
Solution: This is caused by not using Cascade type while defining mapping.