A role of the Adapter strategy is to enable Java or .NET components to transparently access heterogeneous data sources. ScopeEnterprise applications often require connecting to a diverse set of underlying resources. Common proliferation of data access components across an enterprise application yields poor design. With time, the application expands with additional functionality and new integration points. Without a cohesive Integration tier, application maintenance and troubleshooting becomes quite expensive. SolutionAn Adapter component acts as a single gateway to the underlying resources and represents the core of the Data Access Layer. Depending on the type of systems accessed by the application and the underlying technologies such as Java or .NET, the Adapter component encapsulates corresponding data access functionality. An Adapter is also responsible for transactional context propagation as well as error handling. The example demonstrates how to build simple Adapter components in both Java and .NET. This component hides details of the underlying legacy system and enables access to the heterogeneous back-end environment. Figure 6-4 outlines the Integration Tier that is also referred to as the Data Access Layer: Figure 6-4. Data Access Layer
Benefits and LimitationsIn both Java and .NET applications, this strategy can be applied easily to build a DAL. The main advantage of this strategy is the lack of tight dependencies between the underlying resources and the application logic. From a maintenance standpoint, the Adapter component consolidates logic to access an individual resource, which simplifies maintenance and the troubleshooting process. One of the important aspects of the DAL is transactional and security support. Transactional boundaries are particularly critical when a single call spans across multiple resources. Being able to restore data to the original state in case of a failure has to be addressed during the design phase. Similarly, access to the individual back-end applications has to be authenticated and authorized. Building a common security mechanism to protect access to the Resource tier also has to be addressed at design time. Related PatternsThe Data Access Object (DAO) pattern from the Core J2EE Patterns [CJ2EEP] defines an abstraction to encapsulate CRUD operations to the underlying RDBMS. The Adapter strategy can leverage DAO objects to connect to underlying databases. The Adapter strategy goes beyond basic database connectivity, as individual adapters have to connect to legacy systems. The example that follows demonstrates how to implement an Adapter strategy in Java and .NET. Example 1: .NET Data Access AdapterGiven that individual steps involved in creating a connection and executing an SQL statement have already been covered, now it is time to simply put it all together and illustrate how to build an Adapter that alternates between two different databases. In this case it is MySql and SQL Server. From the business logic, if the Warehouse_A, represented with MySql, is offline, an Adapter automatically connects to the Warehouse_B, represented with SQL Server. To execute the .NET Data Access Adapter example, you need to download and place the CoreLab.MySql.dll managed provider for MySql into the corresponding chapter6\lib directory. See [CoreLab] to install the solution. Listing 6-9 lists the C# code for the connectToMySql() method corresponding to the Warehouse_A, MySql connection for the .NET based Data Access Adapter: Listing 6-9. C# Method Connecting to Warehouse_A, MySql Server
Similarly, the connectToSqlServer() method connects the Microsoft SQL Server. The C# implementation of this method is listed in Listing 6-10: Listing 6-10. C# Method Connecting to Warehouse_B, Microsoft SQL Server
Compiling and running the .NET application produces the following output, shown in Listing 6-11: Listing 6-11. Connecting to the Database
Should you disable MySql database, you will see error messages that are followed up with a connection to the SQL Server. Listing 6-12 lists a snippet of the output: Listing 6-12. .NET Data Access Adapter Application Output
The next sections look at the second example where the Data Access Adapter is implemented in Java. Example 2: Java Data Access AdapterThe next task is to build the Java-based implementation of the Data Access Adapter. Notice that there are a fair amount of similarities between Java and .NET implementations. Before proceeding with development, the JDBC drivers for SQL Server and MySql need to be configured. To see the detailed configuration of Connector/J JDBC driver, see [MySql_Config]. To connect to the Microsoft SQL Server, the corresponding JDBC driver [SQLServer_JDBC] needs to be downloaded and installed. Add the following jars to the CLASSPATH: install_dir/lib/msbase.jar install_dir/lib/msutil.jar install_dir/lib/mssqlserver.jar The first step is to connect to the MySql database. Listing 6-13 shows Java code to connect to the Warehouse_A database, MySql server. Listing 6-13. Java Method Connecting to Warehouse_A, MySql
Now establish a similar connection to SQL Server. To specify a connection string, the following information is needed: jdbc:microsoft:sqlserver://hostname:port [;property=value...] Where property=value is required for the database name and user account: Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); Connection conn = DriverManager.getConnection ("jdbc:microsoft:sqlserver://Warehouse_B:1433;User=sa;Password=admin"); Listing 6-14 lists content of the Java main method that incorporates details of accessing the database. Listing 6-14. Java Data Access Adapter Connecting to Warehouse_B, Microsoft SQL Server
Executing the application produces the output listed in Listing 6-15: Listing 6-15. Java Data Access Adapter Application Output, MySql Database
Should MySql be disabled, you will be automatically connecting to SQL Server representing Warehouse_B, as shown in Listing 6-16: Listing 6-16. Java Data Access Adapter Application Output, SQL Server
What has been shown in the preceding code listings are implementations of an Adapter strategy. Java and .NET applications can share an adapter or use different adapters, depending on the Integration tier design. Database connectivity is often accompanied by requirements to connect to legacy systems. Both synchronous and asynchronous integrations are used to connect to legacy applications depending on the scenario. A CRM system may incorporate user information pulled synchronously during use session, while an HR batch update typically happens asynchronously. The next section covers interoperability content related to the synchronous legacy integration. Asynchronous integration is discussed in detail in Chapter 10, "Resource Tier Asynchronous Integration." |