Friday, 15 July 2011

spring - JPA Entity Manager does not throw exception the second time when database is down for the same entity -



spring - JPA Entity Manager does not throw exception the second time when database is down for the same entity -

i using spring crud repository handling database level transactions. web application deployed on tomcat , interacts mysql database. scenario testing involves mysql database going downwards web application deployed on tomcat. jpa entity manager throw exception 1st time same entity(mapped java class) but, sec time same entity, entity manager hangs , throws exception when tomcat shutdown.

i have logs me , if required can upload them.

web.xml

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- processes application requests --> <servlet> <servlet-name>appservlet</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextclass</param-name> <param-value> org.springframework.web.context.support.annotationconfigwebapplicationcontext </param-value> </init-param> <init-param> <param-name>contextconfiglocation</param-name> <param-value> com.test.webconfig </param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>appservlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>cors</filter-name> <filter-class>com.test.filter.corsfilter</filter-class> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>cors</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <error-page> <error-code>404</error-code> <location>/web-inf/pages/404.jsp</location> </error-page> <error-page> <exception-type>java.lang.exception</exception-type> <location>/web-inf/pages/404.jsp</location> </error-page> <error-page> <exception-type>com.test.exceptions.acsexception</exception-type> <location>/web-inf/pages/404.jsp</location> </error-page> </web-app>

webconfig.java

package com.test; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.enableaspectjautoproxy; import org.springframework.scheduling.concurrent.threadpooltaskscheduler; import org.springframework.web.servlet.config.annotation.defaultservlethandlerconfigurer; import org.springframework.web.servlet.config.annotation.enablewebmvc; import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter; import org.springframework.web.servlet.view.internalresourceviewresolver; import org.springframework.web.socket.websockethandler; import org.springframework.web.socket.config.annotation.enablewebsocket; import org.springframework.web.socket.config.annotation.sockjsserviceregistration; import org.springframework.web.socket.config.annotation.websocketconfigurer; import org.springframework.web.socket.config.annotation.websockethandlerregistry; import org.springframework.web.socket.handler.perconnectionwebsockethandler; import org.springframework.web.socket.server.standard.servletservercontainerfactorybean; import com.test.service.handler.agentconnectionhandlerimpl; import com.test.service.handler.customerconnectionhandlerimpl; import com.test.jsonrpc.jsonrpccache; @configuration @enablewebmvc @enablewebsocket @componentscan(basepackages={"com.test"}) //do not enable cglib , i.e. (proxytargetclass=true), default false. @enableaspectjautoproxy public class webconfig extends webmvcconfigureradapter implements websocketconfigurer { private static final logger logger = loggerfactory.getlogger(webconfig.class); @bean public servletservercontainerfactorybean createservletservercontainerfactorybean() { servletservercontainerfactorybean container = new servletservercontainerfactorybean(); container.setmaxtextmessagebuffersize(32768); container.setmaxbinarymessagebuffersize(32768); logger.info("websocket mill returned"); homecoming container; } public void registerwebsockethandlers(websockethandlerregistry registry) { sockjsserviceregistration custsr=registry.addhandler(cobrowsesockjscustomerhandler(), new string[] { "/sockjs/test/customer/" }).withsockjs(); sockjsserviceregistration agentsr=registry.addhandler(cobrowsesockjsagenthandler(), new string[] { "/sockjs/test/agent/" }).withsockjs(); if(custsr!=null){ custsr.setsessioncookieneeded(false); }else{ logger.info("cust sr null"); } if(agentsr!=null){ agentsr.setsessioncookieneeded(false); }else{ logger.info("agent sr null"); } jsonrpccache.getinstance().init("com.avaya.labs"); logger.info("registered sock js"); } @bean public internalresourceviewresolver getinternalresourceviewresolver() { internalresourceviewresolver resolver = new internalresourceviewresolver(); resolver.setprefix("/web-inf/views/"); resolver.setsuffix(".jsp"); homecoming resolver; } @bean public websockethandler cobrowsesockjscustomerhandler() { homecoming new perconnectionwebsockethandler(customerconnectionhandlerimpl.class); } @bean public websockethandler cobrowsesockjsagenthandler() { homecoming new perconnectionwebsockethandler(agentconnectionhandlerimpl.class); } @bean public threadpooltaskscheduler sockjstaskscheduler() { threadpooltaskscheduler taskscheduler = new threadpooltaskscheduler(); taskscheduler.setthreadnameprefix("sockjs-"); homecoming taskscheduler; } public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) { configurer.enable(); } }

repoconfig.java

package com.test; import javax.persistence.entitymanagerfactory; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.context.annotation.propertysource; import org.springframework.core.env.environment; import org.springframework.data.jpa.repository.config.enablejparepositories; import org.springframework.orm.hibernate4.hibernateexceptiontranslator; import org.springframework.orm.jpa.jpadialect; import org.springframework.orm.jpa.jpatransactionmanager; import org.springframework.orm.jpa.localcontainerentitymanagerfactorybean; import org.springframework.orm.jpa.vendor.database; import org.springframework.orm.jpa.vendor.hibernatejpadialect; import org.springframework.orm.jpa.vendor.hibernatejpavendoradapter; import org.springframework.transaction.platformtransactionmanager; import com.jolbox.bonecp.bonecpdatasource; @propertysource(value = "classpath:db.properties") @enablejparepositories("com.test.dao") @configuration public class repoconfig { private static final logger logger = loggerfactory.getlogger(repoconfig.class); @autowired environment env; @bean public bonecpdatasource datasource() { bonecpdatasource bonecpdatasource = null; seek { logger.info("creating bonecpdatasource"); bonecpdatasource = new bonecpdatasource(); bonecpdatasource.setdriverclass(env.getproperty("jdbc.driverclass").trim()); logger.info("jdbs params " + env.getproperty("jdbc.url").trim()); bonecpdatasource.setjdbcurl(env.getproperty("jdbc.url").trim()); bonecpdatasource.setusername(env.getproperty("jdbc.username").trim()); bonecpdatasource.setpassword(env.getproperty("jdbc.password").trim()); bonecpdatasource.setidleconnectiontestperiodinminutes(integer.parseint(env.getproperty( "idleconnectiontestperiodinminute").trim())); bonecpdatasource.setidlemaxageinminutes(integer.parseint(env.getproperty("idlemaxageinminutes").trim())); bonecpdatasource.setmaxconnectionsperpartition(integer.parseint(env .getproperty("maxconnectionperpartition").trim())); bonecpdatasource.setminconnectionsperpartition(integer.parseint(env .getproperty("minconnectionperpartition").trim())); bonecpdatasource.setpartitioncount(integer.parseint(env.getproperty("partitioncount").trim())); bonecpdatasource.setacquireincrement(integer.parseint(env.getproperty("acquireincrement").trim())); bonecpdatasource.setstatementscachesize(integer.parseint(env.getproperty("statementcachesize").trim())); bonecpdatasource.setreleasehelperthreads(integer.parseint(env.getproperty("releasehelperthread").trim())); logger.info("bonecpdatasource created"); } grab (exception e) { logger.error("exception bonecpdatasource", e); } homecoming bonecpdatasource; } @bean public hibernateexceptiontranslator hibernateexceptiontranslator() { logger.info("hibernateexceptiontranslator"); homecoming new hibernateexceptiontranslator(); } @bean @autowired public localcontainerentitymanagerfactorybean entitymanagerfactory() { localcontainerentitymanagerfactorybean mill = null; seek { hibernatejpavendoradapter vendoradapter = new hibernatejpavendoradapter(); // boolean.getboolean(name) boolean generateddl = false; boolean showsql = false; if (env.getproperty("generateddl").trim().equalsignorecase("true")) { generateddl = true; } if (env.getproperty("showsql").trim().equalsignorecase("true")) { showsql = true; } vendoradapter.setgenerateddl(generateddl); vendoradapter.setshowsql(showsql); vendoradapter.setdatabaseplatform(env.getproperty("jdbc.databaseplatform").trim()); string databasetype = env.getproperty("databasetype").trim(); // default mysql database database = database.mysql; // h2, hsql, informix, mysql, oracle, postgresql, sql_server, if (databasetype.equalsignorecase("postgres")) { database = database.postgresql; } else if (databasetype.equalsignorecase("oracle")) { database = database.oracle; } else if (databasetype.equalsignorecase("hsql")) { database = database.hsql; } else if (databasetype.equalsignorecase("sqlserver")) { database = database.sql_server; } vendoradapter.setdatabase(database); logger.info("entitymanagerfactory localcontainerentitymanagerfactorybean"); mill = new localcontainerentitymanagerfactorybean(); factory.setjpavendoradapter(vendoradapter); factory.setpackagestoscan("com.test.dao.model"); logger.info("entitymanagerfactory com.test.dao.model scanned"); bonecpdatasource ds = datasource(); factory.setdatasource(ds); if (ds != null) { logger.info("datasource not null"); } else { logger.info("datasource ## null"); } logger.info("entitymanagerfactory datasource"); // properties properties = new properties(); // properties.setproperty("hibernate.cache.use_second_level_cache", // "true"); // properties.setproperty("hibernate.cache.region.factory_class", // "org.hibernate.cache.ehcache.ehcacheregionfactory"); // properties.setproperty("hibernate.cache.use_query_cache", // "true"); // properties.setproperty("hibernate.generate_statistics", "true"); // // factory.setjpaproperties(properties); // // factory.afterpropertiesset(); logger.info("returning entitymanagerfactory"); homecoming factory; } grab (exception e) { logger.error("exception entitymanagerfactory", e); } homecoming null; } @bean @autowired public platformtransactionmanager transactionmanager() { jpatransactionmanager txmanager = null; seek { entitymanagerfactory emf = entitymanagerfactory().getobject(); if (emf != null) { logger.info("entitymanagerfactory not null"); } else { logger.info("entitymanagerfactory ## null"); } txmanager = new jpatransactionmanager(); jpadialect jpadialect = new hibernatejpadialect(); txmanager.setentitymanagerfactory(emf); txmanager.setjpadialect(jpadialect); } grab (exception e) { logger.error("exception jpatransactionmanager", e); } homecoming txmanager; } }

db.properties

enabledb=false jdbc.url=jdbc:mysql://localhost:3306/acsdb jdbc.username=root jdbc.password=avaya123! jdbc.driverclass=com.mysql.jdbc.driver jdbc.databaseplatform=org.hibernate.dialect.mysql5innodbdialect #connections older sent keep-alive statement. idleconnectiontestperiodinminute=60 # maximum age of unused connection before closed off. idlemaxageinminutes=120 maxconnectionperpartition=30 minconnectionperpartition=10 #connections grouped in partitions decreases contention when opening new connection. partitioncount=1 acquireincrement=5 #min no of prepared statements cache statementcachesize=100 #number of release-connection helper threads create per partition releasehelperthread=3 generateddl=true showsql=false databasetype=mysql

clientconfigdao - dao class issue has been reported

package com.test.dao; import org.springframework.data.repository.crudrepository; import com.test.dao.model.clientconfig; public interface clientconfigdao extends crudrepository<clientconfig, string> { clientconfig findbyclientidandproductversion(string clientid,string productversion); }

clientconfig - entity class

package com.test.dao.model; import java.io.serializable; import java.util.date; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.id; import javax.persistence.table; @entity @table (name="clientconfig") public class clientconfig implements serializable{ private static final long serialversionuid = 1l; @id @column(name = "clientid",nullable=false) private string clientid; @column(name = "productversion",nullable=false) private string productversion="1.0"; @column(name = "maxlicensesagent") private integer numoflicensesagent=50; @column(name = "maxlicensesadmin") private integer numoflicensesadmin=50; //**all values in min @column(name = "agenttimeout") private integer agenttimeout=30; @column(name = "agenttimeoutwarnintv") private integer agenttimeoutwarninterval=5; @column(name = "clienttimeout") private integer clienttimeout=30;//minute @column(name = "clienttimeoutwarnintv") private integer clienttimeoutwarninterval=5; @column(name = "macid") private string macid; @column(name = "hashedvalue") private string hashedvalue; @column(name = "allowedorigins") private string allowedorigins; @column(name = "validitydate") private date validitydate; //these 2 columns temporary arrangement //till time not have pass phrase generator module @column(name = "startpassphrase") private integer startpassphrase=5000; @column(name = "lastpassphrase") private integer lastpassphrase=5000; public clientconfig(){ } public integer getstartpassphrase() { homecoming startpassphrase; } public void setstartpassphrase(integer startpassphrase) { this.startpassphrase = startpassphrase; } public integer getlastpassphrase() { homecoming lastpassphrase; } public void setlastpassphrase(integer lastpassphrase) { this.lastpassphrase = lastpassphrase; } public integer getclienttimeout() { homecoming clienttimeout; } public void setclienttimeout(integer clienttimeout) { this.clienttimeout = clienttimeout; } public integer getnumoflicensesagent() { homecoming numoflicensesagent; } public void setnumoflicensesagent(integer numoflicensesagent) { this.numoflicensesagent = numoflicensesagent; } public integer getnumoflicensesadmin() { homecoming numoflicensesadmin; } public void setnumoflicensesadmin(integer numoflicensesadmin) { this.numoflicensesadmin = numoflicensesadmin; } public string getallowedorigins() { homecoming allowedorigins; } public void setallowedorigins(string allowedorigins) { this.allowedorigins = allowedorigins; } public date getvaliditydate() { homecoming validitydate; } public void setvaliditydate(date validitydate) { this.validitydate = validitydate; } public string getproductversion() { homecoming productversion; } public void setproductversion(string productversion) { this.productversion = productversion; } public string gethashedvalue() { homecoming hashedvalue; } public void sethashedvalue(string hashedvalue) { this.hashedvalue = hashedvalue; } public string getmacid() { homecoming macid; } public void setmacid(string macid) { this.macid = macid; } public string getclientid() { homecoming clientid; } public void setclientid(string clientid) { this.clientid = clientid; } public integer getagenttimeout() { homecoming agenttimeout; } public void setagenttimeout(integer agenttimeout) { this.agenttimeout = agenttimeout; } public integer getagenttimeoutwarninterval() { homecoming agenttimeoutwarninterval; } public void setagenttimeoutwarninterval(integer agenttimeoutwarninterval) { this.agenttimeoutwarninterval = agenttimeoutwarninterval; } public integer getclienttimeoutwarninterval() { homecoming clienttimeoutwarninterval; } public void setclienttimeoutwarninterval(integer clienttimeoutwarninterval) { this.clienttimeoutwarninterval = clienttimeoutwarninterval; } }

bonecp not seem homecoming exception underlying spring layer error handling. instead keeps on trying connection database until connection established or tomcat process killed.

spring jpa bonecp

No comments:

Post a Comment