Welcome!

Java Authors: Lori MacVittie, Roger Strukhoff, Esmeralda Swartz, Trevor Parsons, Sandi Mappic

Related Topics: Java

Java: Article

An Introduction to Service Data Objects

Integrating relational data into Web applications

Late last year, IBM Corp., and BEA Systems, Inc., introduced Service Data Objects (SDO), a new data programming specification that complements existing Java 2 Enterprise Edition technologies and enables service-oriented architectures by providing uniform data access for a wide variety of service and resource types. Not only does SDO enable a consistent approach to data access, but it also provides features that simplify common application tasks, such as allowing data browsing and update while the application is disconnected from the data source.

In this article we explain how SDO works and where it may fit into your own applications. We also take a closer look at how you can use SDO to retrieve and modify data stored in a relational database.

SDO Concepts
At the core of SDO are DataGraphs and Data Mediator Services (DMS). In simple terms, an application program obtains a DataGraph from a DMS that is specific to some back-end data store. The program can then examine and/or update the data contained in the graph. Optionally the program can employ a DMS to propagate the entire set of updates back to the original data source.

DataGraph
As a program object, a DataGraph provides an in-memory, nonpersistent copy of data. A program can work with this data even when there's no connection to the original data source. The data in a DataGraph is organized as a group of DataObjects, which may be linked together (i.e., the data is structured as a "graph"). A DataGraph also contains a schema that describes the structure of the DataObject type(s) contained in the DataGraph. To handle updates, a DataGraph also maintains a change history to track all modifications made to the graph.

Each DataObject contains a set of properties, which can be primitive values or references to other DataObjects contained in the DataGraph. If a DataObject's schema is known at development time, a developer can use automated tools to generate typed interfaces that simplify DataObject access. Alternatively, the application can define the schema at runtime, allowing dynamic access of DataObjects. With either static or dynamic access, linked data can be accessed in either a breadth-first or depth-first manner. For example, if a DataGraph contains customers and related orders, then orders can be obtained directly from the DataGraph or from their parent customer DataObject. SDO also allows for accessing data through XML Path Language (XPath) subset expressions.

Disconnected Programming Model
The DataGraph's disconnected, data source-independent nature provides a simple programming model, supports common application patterns, and offers a potential performance advantage.

Today, J2EE application developers have a wide variety of persistence frameworks to choose from, such as JDBC, EJB, or JDO. These frameworks have different APIs and are often complex, requiring developers to spend a great deal of time learning multiple APIs rather than developing applications. Since SDO provides a single data access API regardless of the persistence mechanism, developers can choose the framework that best fits an application without using different APIs.

Some developers strive for similar independence by developing custom Java objects to encapsulate data from different data sources. This tactic makes use of the Data Access Object design pattern. SDO inherently supports this pattern, freeing developers from the need to develop their own infrastructure.

To improve performance, some types of applications can exploit the DataGraph's support for applying multiple updates in one method call to reduce the number of connections and/or database operations. By storing data from multiple database rows and tables in a DataGraph, applications can make changes to the data without making additional round-trips to the database.

Mediators
An SDO DataGraph must be populated from a data source or a service. The SDO component that populates a DataGraph is called a data mediator service (DMS). A DMS also propagates changes to the in-memory DataGraph back to the originating data source. Note that the current SDO specification does not define a specific API for DMSs - beyond a few basic requirements, each DMS provider is free to design the DMS that best suits the associated data source.

Typically, a DMS accesses a single type of data source, for example, JDBC resources or entity EJBs. All DMSs require the developer to provide a description of the data to be accessed. This data description (or metadata) typically consists of a schema and a query over the associated data source.

Figure 1, from the SDO specification, illustrates the flow of data during a typical interaction between an SDO client and a DMS. The client makes a request to the DMS to return a DataGraph. The DMS reads the requested data from the data source, constructs a DataGraph of related DataObjects, and returns the DataGraph to the application. The SDO client makes changes to the DataGraph in-memory and then sends the modified DataGraph back to the DMS. The DMS examines the ChangeSummary contained in the graph and propagates the changes back to the original data source.

Because the DataGraph is disconnected from the data source, it's possible that another application will update the data (in the data source) that was used to populate a DataGraph before the application requests the DMS to propagate the application's changes back to the data source. To handle such potential update conflicts, a DMS typically implements some form of "optimistic" concurrency control and throws an exception to the application when a data collision occurs. At that point, it is the application's responsibility to recover from the collision, for example, by rereading the data and restarting the transaction.

Too-frequent collisions under an optimistic concurrency approach can degrade performance as well as aggravate end users. In applications where multiple applications will often attempt concurrent changes to the same data, optimistic concurrency control may not be a good choice. However, for applications without this behavior, optimistic concurrency control can improve performance by reducing lock contention.

Metamodel
The SDO specification assumes the presence of a metamodel and metadata API for the DataGraph, but does not specify one explicitly. Today, SDO could be implemented with a variety of metamodels and schema languages such as XML Schema or the Essential Meta Object Facility (EMOF). The metamodel implementation does not affect SDO end users.

XML Serialization
SDO defines the XML format for DataGraphs and DataObjects, and specifies that the format can be customized by an XSD. This same format is used for Java serialization. The serialized form of a DataGraph includes the DataObjects as well as the schema and change summary. This capability allows data to be easily transferred over the wire as would be required by a Web service invocation.

Relationship to Other J2EE Technologies
SDO can complement or simplify existing J2EE technologies. SDO complements JDBC by providing a more powerful framework and API for data access. In a typical relational database, data is normalized into multiple tables. When this data is read using a join query through JDBC, it's returned to the application in a tabular format that includes some data redundantly (e.g., an order number may be repeated with all individual line items for the same order). This format doesn't directly correspond to Java's object-oriented data model and can complicate navigation and update operations. A JDBC DMS can restructure this tabular data into a graph of related DataObjects. For example, an order might be represented by a DataObject that contains a list of references to other DataObjects containing line-item data. This allows an application to use standard Java techniques to access and modify the data.

Data access via EJBs can also be enhanced by using SDO. To implement a disconnected Data Access Object design pattern with EJBs alone, an application must use some combination of copy helper objects, session beans, and EJB access beans. An EJB DMS provides a ready-to-use disconnected architecture and frees developers from having to implement their own framework or custom artifacts.

SDO could also be used to complement other related technologies. For example:

  • JSR 227: Declaratively binding and accessing data in J2EE applications. SDO could be used as the mechanism to return results from the data source.
  • JSR 225: XQuery API for Java (XQJ). A Data Mediator Service could use the provided API to return SDOs.
  • JDO 2.0: SDO could provide data transfer objects from JDO persistent objects.
  • WEB UI Data Binding: JSF can use SDOs as a data binding. JSTL can use an SDO DataObject impl that implements the map interface.
Security
Security is not part of the current SDO model, so security in an SDO-based application is provided at the edges. For example, if an SDO-based application is developed that employs an EJB session bean and a JDBC connection, then security is provided at the boundaries of the application by these two J2EE components.

SDO with JDBC
SDO provides a uniform model for accessing data from a variety of services or data sources, including JDBC. Figure 2 shows interactions among the main artifacts involved when an application uses SDO over JDBC. Notice how the application code calls mediator and DataGraph methods, while the mediator calls JDBC and DataGraph methods, thus insulating the application from JDBC.

There are three central aspects to using a JDBC mediator: metadata, connections, and transaction handling.

Metadata
The application must supply the JDBC DMS with a metadata object that specifies the data to be retrieved. For a JDBC mediator, the metadata contains an augmented relational database schema that defines a set of tables, their relationships, and selection and ordering criteria. The JDBC DMS creates a DataGraph that corresponds to the provided schema definition. Each DataObject type within the DataGraph corresponds to a table definition in the schema, and each DataObject property corresponds to a table column.

The JDBC DMS uses the metadata to generate a SQL Select statement to retrieve data for the DataGraph. The simplest metadata example would describe a single table and no selection or ordering. For this specification, the JDBC mediator would retrieve all rows of the table and create a DataGraph that contains a list of DataObjects, with each DataObject containing the data from one row in the table. Each DataObject will have a set of values corresponding to the values from each column.

A more complex example might involve two related tables; say Customers and their respective Orders. In this case, the metadata must specify the relationship between the two tables, which will subsequently be reflected in a corresponding relationship between two types of DataObjects.

The DataGraph returned in this case would contain a list of Customer DataObjects and each of these Customer DataObjects would have a list of related Order DataObjects. The DataGraph will contain all Customers and Orders; they are organized as a tree with Customers at the "root" of the tree and related Orders branching off of each Customer.

Applications will frequently want data only from specified rows of a table. In this case, the metadata for a JDBC DMS specifies selection criteria. For example, customers might be selected from a particular zip code or with a particular last name. Also, the DataObjects in the graph can optionally be ordered by specifying "order by" columns in the metadata.

Normally the JDBC DMS generates SQL select, insert, update, and delete statements to read and update the associated relational database. However, an application can optionally provide an explicit Select statement for the mediator to use. If this option is used, the DMS will then generate only the complementary insert, update, and delete statements and will use the provided select statement as is.

Connections
In addition to specifying what data to retrieve, an application must specify which data source the DMS should access. For a JDBC DMS, this can be done by specifying a JDBC Connection object. The DMS will use this connection for all database interactions.

Transactions
As mentioned earlier, SDO provides a disconnected programming model and, accordingly, DMSs will typically connect to a data store only to read data for graph creation or to write data to reflect changes back to the store.

When an application requests the JDBC DMS to retrieve data and produce a DataGraph, the DMS starts a transaction, reads the data, creates the graph, and ends the transaction. The DataGraph is returned to the application and is "disconnected" in the sense that it is not associated with any connection or transaction; there are no locks held on the data.

The client can now read data from the DataGraph and make changes to it while it is in memory and disconnected from the data source. All changes made to the graph are recorded by the DataGraph. At some point the client will want to push these changes back to the data source and call the JDBC DMS "applyChanges" API.

As part of the "applyChanges" function, the JDBC DMS will reflect to the data store all changes made to the graph as part of a single transaction; this is true whether there is a single change to the graph or an entire batch of changes.

The disconnected programming model generally implies the use of an optimistic concurrency control scheme to push changes back to the data store; this is the approach taken by the JDBC DMS.

When the DMS attempts to apply DataGraph changes back to the data store, each row being updated is checked to ensure it has not been modified since it was originally read. If no intervening modifications have taken place, the update proceeds. If a row has been modified since the data was read, a collision has occurred; the update transaction is rolled back and an exception is thrown to the client.

There is also an option to use the DMS within a larger transaction. If this option is used, the DMS will assume that the client is controlling the transaction and will not perform any commit or rollback operations.

An Example
The following simple example demonstrates JDBC database access with SDO employing the JDBC DMS. This example has six steps, each illustrated by a code snippet.

Step 1: Create the JDBC mediator metadata instance
Create the metadata instance to represent the Customer table. This example demonstrates the creation of the JDBC DMS metadata programmatically. This is an obvious candidate for tooling support. Remember that the JDBC DMS uses a simple mapping scheme whereby each table definition results in a DataObject type and each table column results in a DataObject type property (see Listing 1).

Step 2: Create the DMS instance as in Listing 2

Step 3: Read the DataGraph from the database


//Create the "lastName" argument for the filter predicate
DataObject arguments = mediator.getParameterDataObject();
arguments.put("CUSTLASTNAME", "Pavick");
DataObject graph = mediator.getGraph(arguments);

Step 4 : Retrieve data from the graph


//Iterate through all returned customers and print the first name
Iterator i = graph.getList("CUSTOMER").iterator();
	while (i.hasNext()) {
	DataObject cust = (DataObject) i.next();
	System.out.println(cust.getString("CUSTFIRSTNAME"));
	}

Step 5: Change the DataGraph


List customers = graph.getList("CUSTOMER");
//Get the first customer in the graph and update the name
DataObject customer = (DataObject)customers.get(0);
	customer.setString("CUSTFIRSTNAME", "Kevin");

Step 6: Apply DataGraph changes back to the database

mediator.applyChanges(graph);

Variations on the Example
Metadata File

In Step 1 we created the mediator metadata programmatically. An alternative is to provide the metadata in the form of an XML file. Listing 3 is the XML representation of the Customer metadata.

Using this file, Step 1 would not be necessary and Step 2 would become:

Step 2: Create the mediator instance as shown in Listing 4

Static Types
The example provided above uses the dynamic access APIs of DataOb-ject. The JDBC DMS also supports the use of static SDO types. To use the static API access to DataObjects, a set of static types is generated at development time and tools are provided for this purpose. Using static types provides a cleaner user API as well as a performance boost at runtime. The generation step is beyond the scope of this article, but this is what Step 4 looks like when using a static customer DataObject.

Step 4: Retrieve data from the graph


//Iterate through all returned customers and print the first name
Iterator i = graph.getCustomers().iterator();
	while (i.hasNext()) {
	Customer cust = (Customer) i.next();
	System.out.println(cust.getFirstName());
	}

Paging
The JDBC Data Mediator Service also provides a paging capability that can be useful for marching through large data sets. A pager interface provides a cursor-like next() capability. The next() function returns a graph representing the next page of data from the entire data set specified by the mediator metadata; a previous() function is also available. A CountingPager is also provided that allows the retrieval of a specified page from the data set.

Listing 5 illustrates paging through a large set of customer instances using a Counting Pager.

Conclusion
In this article we have explored some of the key SDO concepts and also drilled down into a specific use of the technology for relational database access employing a JDBC Data Mediator.

SDO is a standard from IBM and BEA and there is a reference implementation under development at www.eclipse.org/emf. This EMF-based implementation of SDO will also be delivered with WebSphere Application Server 6.0 and will be complemented by:

  • JDBC Data Mediator Service
  • EJB Data Mediator Service
It is anticipated that the 6.0 version of WebSphere Studio will contain complete support for creating applications that leverage SDO; this will include visual tooling to configure the JDBC DMS. With the power of SDO, relational data can be integrated into Web applications more easily than ever.

Acknowledgments
We would like to thank Stephen Brodsky and Tom Schneider for their assistance with this article.

More Stories By Kevin Williams

Kevin Williams is a software developer with IBM and is leading IBM’s participation in the DAS subproject of the Apache Tuscany incubator.

More Stories By Brent Daniel

Brent Daniel is a software developer with IBM. He currently works on a JDBC data mediator service for WebSphere Application Server.

Comments (2)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
The Internet of Things will greatly expand the opportunities for data collection and new business models driven off of that data. In her session at Internet of @ThingsExpo, Esmeralda Swartz, CMO of MetraTech, will discuss how for this to be effective you not only need to have infrastructure and operational models capable of utilizing this new phenomenon, but increasingly service providers will need to convince a skeptical public to participate. Get ready to show them the money! Speaker Bio: Esmeralda Swartz, CMO of MetraTech, has spent 16 years as a marketing, product management, and busin...
Samsung VP Jacopo Lenzi, who headed the company's recent SmartThings acquisition under the auspices of Samsung's Open Innovaction Center (OIC), answered a few questions we had about the deal. This interview was in conjunction with our interview with SmartThings CEO Alex Hawkinson. IoT Journal: SmartThings was developed in an open, standards-agnostic platform, and will now be part of Samsung's Open Innovation Center. Can you elaborate on your commitment to keep the platform open? Jacopo Lenzi: Samsung recognizes that true, accelerated innovation cannot be driven from one source, but requires a...
SYS-CON Events announced today that Red Hat, the world's leading provider of open source solutions, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Red Hat is the world's leading provider of open source software solutions, using a community-powered approach to reliable and high-performing cloud, Linux, middleware, storage and virtualization technologies. Red Hat also offers award-winning support, training, and consulting services. As the connective hub in a global network of enterprises, partners, a...
P2P RTC will impact the landscape of communications, shifting from traditional telephony style communications models to OTT (Over-The-Top) cloud assisted & PaaS (Platform as a Service) communication services. The P2P shift will impact many areas of our lives, from mobile communication, human interactive web services, RTC and telephony infrastructure, user federation, security and privacy implications, business costs, and scalability. In his session at Internet of @ThingsExpo, Robin Raymond, Chief Architect at Hookflash Inc., will walk through the shifting landscape of traditional telephone a...
SYS-CON Events announced today that Matrix.org has been named “Silver Sponsor” of Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Matrix is an ambitious new open standard for open, distributed, real-time communication over IP. It defines a new approach for interoperable Instant Messaging and VoIP based on pragmatic HTTP APIs and WebRTC, and provides open source reference implementations to showcase and bootstrap the new standard. Our focus is on simplicity, security, and supporting the fullest feature set.
BSQUARE is a global leader of embedded software solutions. We enable smart connected systems at the device level and beyond that millions use every day and provide actionable data solutions for the growing Internet of Things (IoT) market. We empower our world-class customers with our products, services and solutions to achieve innovation and success. For more information, visit www.bsquare.com.
How do APIs and IoT relate? The answer is not as simple as merely adding an API on top of a dumb device, but rather about understanding the architectural patterns for implementing an IoT fabric. There are typically two or three trends: Exposing the device to a management framework Exposing that management framework to a business centric logic • Exposing that business layer and data to end users. This last trend is the IoT stack, which involves a new shift in the separation of what stuff happens, where data lives and where the interface lies. For instance, it’s a mix of architectural style...
SYS-CON Events announced today that SOA Software, an API management leader, will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. SOA Software is a leading provider of API Management and SOA Governance products that equip business to deliver APIs and SOA together to drive their company to meet its business strategy quickly and effectively. SOA Software’s technology helps businesses to accelerate their digital channels with APIs, drive partner adoption, monetize their assets, and achieve a...
From a software development perspective IoT is about programming "things," about connecting them with each other or integrating them with existing applications. In his session at @ThingsExpo, Yakov Fain, co-founder of Farata Systems and SuranceBay, will show you how small IoT-enabled devices from multiple manufacturers can be integrated into the workflow of an enterprise application. This is a practical demo of building a framework and components in HTML/Java/Mobile technologies to serve as a platform that can integrate new devices as they become available on the market.
SYS-CON Events announced today that Utimaco will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Utimaco is a leading manufacturer of hardware based security solutions that provide the root of trust to keep cryptographic keys safe, secure critical digital infrastructures and protect high value data assets. Only Utimaco delivers a general-purpose hardware security module (HSM) as a customizable platform to easily integrate into existing software solutions, embed business logic and build s...
Connected devices are changing the way we go about our everyday life, from wearables to driverless cars, to smart grids and entire industries revolutionizing business opportunities through smart objects, capable of two-way communication. But what happens when objects are given an IP-address, and we rely on that connection, sometimes with our lives? How do we secure those vast data infrastructures and safe-keep the privacy of sensitive information? This session will outline how each and every connected device can uphold a core root of trust via a unique cryptographic signature – a “bir...
Internet of @ThingsExpo Silicon Valley announced on Thursday its first 12 all-star speakers and sessions for its upcoming event, which will take place November 4-6, 2014, at the Santa Clara Convention Center in California. @ThingsExpo, the first and largest IoT event in the world, debuted at the Javits Center in New York City in June 10-12, 2014 with over 6,000 delegates attending the conference. Among the first 12 announced world class speakers, IBM will present two highly popular IoT sessions, which will take place November 4-6, 2014 at the Santa Clara Convention Center in Santa Clara, Calif...
Almost everyone sees the potential of Internet of Things but how can businesses truly unlock that potential. The key will be in the ability to discover business insight in the midst of an ocean of Big Data generated from billions of embedded devices via Systems of Discover. Businesses will also need to ensure that they can sustain that insight by leveraging the cloud for global reach, scale and elasticity.
WebRTC defines no default signaling protocol, causing fragmentation between WebRTC silos. SIP and XMPP provide possibilities, but come with considerable complexity and are not designed for use in a web environment. In his session at Internet of @ThingsExpo, Matthew Hodgson, technical co-founder of the Matrix.org, will discuss how Matrix is a new non-profit Open Source Project that defines both a new HTTP-based standard for VoIP & IM signaling and provides reference implementations.

SUNNYVALE, Calif., Oct. 20, 2014 /PRNewswire/ -- Spansion Inc. (NYSE: CODE), a global leader in embedded systems, today added 96 new products to the Spansion® FM4 Family of flexible microcontrollers (MCUs). Based on the ARM® Cortex®-M4F core, the new MCUs boast a 200 MHz operating frequency and support a diverse set of on-chip peripherals for enhanced human machine interfaces (HMIs) and machine-to-machine (M2M) communications. The rich set of periphera...

SYS-CON Events announced today that Aria Systems, the recurring revenue expert, has been named "Bronze Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Aria Systems helps leading businesses connect their customers with the products and services they love. Industry leaders like Pitney Bowes, Experian, AAA NCNU, VMware, HootSuite and many others choose Aria to power their recurring revenue business and deliver exceptional experiences to their customers.
The Internet of Things (IoT) is going to require a new way of thinking and of developing software for speed, security and innovation. This requires IT leaders to balance business as usual while anticipating for the next market and technology trends. Cloud provides the right IT asset portfolio to help today’s IT leaders manage the old and prepare for the new. Today the cloud conversation is evolving from private and public to hybrid. This session will provide use cases and insights to reinforce the value of the network in helping organizations to maximize their company’s cloud experience.
The Internet of Things (IoT) is making everything it touches smarter – smart devices, smart cars and smart cities. And lucky us, we’re just beginning to reap the benefits as we work toward a networked society. However, this technology-driven innovation is impacting more than just individuals. The IoT has an environmental impact as well, which brings us to the theme of this month’s #IoTuesday Twitter chat. The ability to remove inefficiencies through connected objects is driving change throughout every sector, including waste management. BigBelly Solar, located just outside of Boston, is trans...
SYS-CON Events announced today that Matrix.org has been named “Silver Sponsor” of Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Matrix is an ambitious new open standard for open, distributed, real-time communication over IP. It defines a new approach for interoperable Instant Messaging and VoIP based on pragmatic HTTP APIs and WebRTC, and provides open source reference implementations to showcase and bootstrap the new standard. Our focus is on simplicity, security, and supporting the fullest feature set.
Predicted by Gartner to add $1.9 trillion to the global economy by 2020, the Internet of Everything (IoE) is based on the idea that devices, systems and services will connect in simple, transparent ways, enabling seamless interactions among devices across brands and sectors. As this vision unfolds, it is clear that no single company can accomplish the level of interoperability required to support the horizontal aspects of the IoE. The AllSeen Alliance, announced in December 2013, was formed with the goal to advance IoE adoption and innovation in the connected home, healthcare, education, aut...