Welcome!

Java IoT Authors: Yeshim Deniz, Liz McMillan, Elizabeth White, Pat Romanski, Frank Lupo

Related Topics: Java IoT, Recurring Revenue

Java IoT: Blog Feed Post

ADF Fail Whale: Handling the Database Is Down Gracefully

To most users a JDBC error would mean nothing, particularly if your app is delivered to the general public on the Internet

Java Developer Magazine on Ulitzer

(JDev 11g 11.1.1.2.0) Like most Oracle applications, when an ADF application loses its connection to the database, the game's up. There's really not much you can do. ADF does detect this situation, presenting the following popup to the user:

But to most users a JDBC error would mean nothing, particularly if your application is delivered to the general public on the internet. In turn the user is left in the application without the ability to do much, resulting actions showing the same popup JDBC error again.

Ideally what we'd like to do is redirect to a web page that gives more useful information, maybe something like the famous Twitter Fail Whale:

The following blog post shows you a solution to do just this. This solution is based on Steve Muench's Dynamic JDBC Credentials example #129. In addition I must give my thanks to Oracle Support for pointing me to Steve's solution.

As usual, please note this solution has yet to be proven in a production environment. Seriously, I've run some arbitrary tests to see if the technique works, but no idea if it'll cover all situations where the database goes down. It's important if you take this example that you test it to ensure it meets your own needs. I'd appreciate it if anybody who does find any issues and resulting solutions, if you could please post them on this post as a comment to assist other readers.

With the following solution I'm not going to bother to explain all the moving parts, just give you the code and where it goes. I'll leave the reader to follow up with their own research on the mechanics of this solution.

All the following work is undertaken in the ViewController project:

1) New class: JdbcDCErrorHandlerImpl.java

package view;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.sql.SQLException;

import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCErrorHandlerImpl;

import oracle.jbo.DMLException;
import oracle.jbo.common.JBOClass;

/*
* Example sourced from Steve Muench example #129
*/
public class JdbcDCErrorHandlerImpl extends DCErrorHandlerImpl {
public JdbcDCErrorHandlerImpl() {
super(true);
}

public JdbcDCErrorHandlerImpl(boolean b) {
super(b);
}

private static final int INVALID_USERNAME_PASSWORD_ ORACLE_ERROR = 1017;
private static final int ACCOUNT_LOCKED_ORACLE_ERROR = 28000;
private static final int NO_SUITABLE_DRIVER = 0;
private static final int NETWORK_CONNECTION_ERROR = 17002;
private static final int NETWORK_ADAPTOR_ERROR = 20;

public static boolean isFailedDBConnectErrorCode(SQLException s) {
int errorCode = s.getErrorCode();
return (errorCode == INVALID_USERNAME_PASSWORD_ORACLE_ ERROR || errorCode == ACCOUNT_LOCKED_ORACLE_ERROR ||
errorCode == NO_SUITABLE_DRIVER || errorCode == NETWORK_ CONNECTION_ERROR ||
errorCode == NETWORK_ADAPTOR_ERROR);
}

@Override
public void reportException(DCBindingContainer formBnd, Exception e) {
super.reportException(formBnd, e);
if (e instanceof DMLException) {
Object[] details = ((DMLException)e).getDetails();
if (details != null && details.length > 0) {
if (details[0] instanceof SQLException) {
SQLException s = (SQLException)details[0];
int errorCode = s.getErrorCode();
if (isFailedDBConnectErrorCode(s)) {
markResponseCompleteIfUsingJSF();
throw (DMLException)e;
}
}
}
}
}

/*
* If we are running in a Faces environment, invoke the FacesContext.responseComplete() method after
* the session invalidate. We use Java reflection so that our code can still work in a Non-Faces environment, too.
*/
private void markResponseCompleteIfUsingJSF() {
try {
Class c = JBOClass.forName("javax.faces.context.FacesContext");
Method m = c.getMethod("getCurrentInstance", null);
Object obj = m.invoke(null, null);
if (obj != null) {
m = c.getMethod("responseComplete", null);
m.invoke(obj, null);
}
} catch (InvocationTargetException ex) {
throw new RuntimeException(ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
} catch (NoSuchMethodException ex) {
throw new RuntimeException(ex);
} catch (ClassNotFoundException ex) {
// Ignore, we're not running in a faces context.
}
}
}

2) New class: JdbcPagePhaseListener.java

package view;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import oracle.adf.controller.v2.lifecycle.Lifecycle;
import oracle.adf.controller.v2.lifecycle.PagePhaseEvent;
import oracle.adf.controller.v2.lifecycle.PagePhaseListener;
import oracle.adf.model.bc4j.DCJboDataControl;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCDataControl;
import oracle.adf.model.binding.DCExecutableBinding;
import oracle.adf.model.binding.DCIteratorBinding;

import oracle.binding.DataControl;

import oracle.jbo.uicli.binding.JUControlBinding;
import oracle.jbo.uicli.binding.JUCtrlActionBinding;

/*
* Example sourced from Steve Muench example #129
*/
public class JdbcPagePhaseListener implements PagePhaseListener {
public JdbcPagePhaseListener() {
}

public void afterPhase(PagePhaseEvent event) {
}

private HttpSession getHttpSession(PagePhaseEvent event) {
return ((HttpServletRequest)event.getLifecycleContext(). getEnvironment().getRequest()).getSession(true);
}

public void beforePhase(PagePhaseEvent event) {
if (event.getPhaseId() == Lifecycle.PREPARE_MODEL_ID) {
DCBindingContainer bc = (DCBindingContainer)event. getLifecycleContext().getBindingContainer();
// Force the Data Control to be referenced before the prepareModel phase to cause the possible JDBC connection
// failure to be signalled now instead of during the page rendering.
List dcList = getADFBCDataControlsList(bc);
}
}

/*
* Return a list of ADFBC data controls used by this page. See how this is used in the beforePhase method above.
*/
private List getADFBCDataControlsList(DCBindingContainer bc) {
List dcList = null;
// if bc == null, means non data bound page, as such no data controls to exercise
if (bc != null) {
dcList = new ArrayList();
List ctrlBindings = (List)bc.getControlBindings();
if (ctrlBindings != null) {
for (JUControlBinding ctrlBinding : ctrlBindings) {
DCIteratorBinding iter = ctrlBinding.getIteratorBinding();
DCDataControl dc = null;
if (iter != null) {
dc = iter.getDataControl();
} else if (ctrlBinding instanceof JUCtrlActionBinding) {
dc = ((JUCtrlActionBinding)ctrlBinding).getDataControl();
}
if (dc != null && dc instanceof DCJboDataControl && !dcList.contains(dc)) {
DCJboDataControl bcdc = (DCJboDataControl)dc;
dcList.add(bcdc);
}
}
}
List exeBindings = (List)bc.getIterBindingList();
if (exeBindings != null) {
for (DCExecutableBinding exeBinding : exeBindings) {
DataControl dc = null;
if (exeBinding instanceof DCIteratorBinding) {
dc = ((DCIteratorBinding)exeBinding).getDataControl();
}
if (dc != null && dc instanceof DCJboDataControl && !dcList.contains(dc)) {
DCJboDataControl bcdc = (DCJboDataControl)dc;
dcList.add(bcdc);
}
}
}
}
return dcList;
}
}

3) New file: ViewController/adfmsrc/META-INF/adf-settings.xml

JdbcPagePhaseListener
view.JdbcPagePhaseListener


4) Modify the DataBinding.cpx file ErrorHandlerClass property

SeparateXMLFiles="false" Package="view" ClientType="Generic" ErrorHandlerClass="view.JdbcDCErrorHandlerImpl">

5) Add an error-page entry in the web.xml

oracle.jbo.DMLException
/ServiceUnavailable.html

6) Add the corresponding html page from the last entry, displaying whatever friendly error message you want to show.

Read the original blog entry...

More Stories By Chris Muir

Chris Muir, an Oracle ACE Director, senior developer and trainer, and frequent blogger at http://one-size-doesnt-fit-all.blogspot.com, has been hacking away as an Oracle consultant with Australia's SAGE Computing Services for too many years. Taking a pragmatic approach to all things Oracle, Chris has more recently earned battle scars with JDeveloper, Apex, OID and web services, and has some very old war-wounds from a dark and dim past with Forms, Reports and even Designer 100% generation. He is a frequent presenter and contributor to the local Australian Oracle User Group scene, as well as a contributor to international user group magazines such as the IOUG and UKOUG.

@ThingsExpo Stories
SYS-CON Events announced today that Dasher Technologies will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Dasher Technologies, Inc. ® is a premier IT solution provider that delivers expert technical resources along with trusted account executives to architect and deliver complete IT solutions and services to help our clients execute their goals, plans and objectives. Since 1999, we'v...
SYS-CON Events announced today that NetApp has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp emp...
SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
SYS-CON Events announced today that TidalScale, a leading provider of systems and services, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale has been involved in shaping the computing landscape. They've designed, developed and deployed some of the most important and successful systems and services in the history of the computing industry - internet, Ethernet, operating s...
Infoblox delivers Actionable Network Intelligence to enterprise, government, and service provider customers around the world. They are the industry leader in DNS, DHCP, and IP address management, the category known as DDI. We empower thousands of organizations to control and secure their networks from the core-enabling them to increase efficiency and visibility, improve customer service, and meet compliance requirements.
SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
Join IBM November 1 at 21st Cloud Expo at the Santa Clara Convention Center in Santa Clara, CA, and learn how IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Cognitive analysis impacts today’s systems with unparalleled ability that were previously available only to manned, back-end operations. Thanks to cloud processing, IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Imagine a robot vacuum that becomes your personal assistant tha...
In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, will lead you through the exciting evolution of the cloud. He'll look at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering ...
In a recent survey, Sumo Logic surveyed 1,500 customers who employ cloud services such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP). According to the survey, a quarter of the respondents have already deployed Docker containers and nearly as many (23 percent) are employing the AWS Lambda serverless computing framework. It’s clear: serverless is here to stay. The adoption does come with some needed changes, within both application development and operations. Tha...
SYS-CON Events announced today that Avere Systems, a leading provider of enterprise storage for the hybrid cloud, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere delivers a more modern architectural approach to storage that doesn't require the overprovisioning of storage capacity to achieve performance, overspending on expensive storage media for inactive data or the overbui...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
SYS-CON Events announced today that TidalScale will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. TidalScale is the leading provider of Software-Defined Servers that bring flexibility to modern data centers by right-sizing servers on the fly to fit any data set or workload. TidalScale’s award-winning inverse hypervisor technology combines multiple commodity servers (including their ass...
SYS-CON Events announced today that N3N will exhibit at SYS-CON's @ThingsExpo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. N3N’s solutions increase the effectiveness of operations and control centers, increase the value of IoT investments, and facilitate real-time operational decision making. N3N enables operations teams with a four dimensional digital “big board” that consolidates real-time live video feeds alongside IoT sensor data a...
As hybrid cloud becomes the de-facto standard mode of operation for most enterprises, new challenges arise on how to efficiently and economically share data across environments. In his session at 21st Cloud Expo, Dr. Allon Cohen, VP of Product at Elastifile, will explore new techniques and best practices that help enterprise IT benefit from the advantages of hybrid cloud environments by enabling data availability for both legacy enterprise and cloud-native mission critical applications. By rev...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
Join IBM November 1 at 21st Cloud Expo at the Santa Clara Convention Center in Santa Clara, CA, and learn how IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Cognitive analysis impacts today’s systems with unparalleled ability that were previously available only to manned, back-end operations. Thanks to cloud processing, IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Imagine a robot vacuum that becomes your personal assistant th...
Digital transformation is changing the face of business. The IDC predicts that enterprises will commit to a massive new scale of digital transformation, to stake out leadership positions in the "digital transformation economy." Accordingly, attendees at the upcoming Cloud Expo | @ThingsExpo at the Santa Clara Convention Center in Santa Clara, CA, Oct 31-Nov 2, will find fresh new content in a new track called Enterprise Cloud & Digital Transformation.
Most technology leaders, contemporary and from the hardware era, are reshaping their businesses to do software. They hope to capture value from emerging technologies such as IoT, SDN, and AI. Ultimately, irrespective of the vertical, it is about deriving value from independent software applications participating in an ecosystem as one comprehensive solution. In his session at @ThingsExpo, Kausik Sridhar, founder and CTO of Pulzze Systems, will discuss how given the magnitude of today's applicati...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...