This post shows how the Connector and Metadata Accelerator is used to access MySQL DB from Pega 7 application. This wizard will automatically generate the necessary activity rules for accessing external MySQL database tables and for mapping query input parameters and query results.
- For this example, the MySQL Community Server 5.7.12 for Windows x64 was used.
- The MySQL DB Server can be downloaded from the MySQL Community Server website.
- In addition, the MySQL Workbench, a MySQL development and administration tool was used.
- It can be downloaded for free from the MySQL Workbench website.
Summary
- Pega 7 Setup Used in this Example
- MySQL DB Server Configuration and Table Used in this Example
- Configuration of Connection to MySQL Server in Pega 7
- Creating a new Database Instance in Pega 7 for MySQL JDBC Access
- Running the Pega 7 Connector and Metadata Accelerator
- Access MySQL DB from Pega 7 Application with an Activity Rule
Related Posts
- How to Access Oracle DB from Pega 7
- How to Access MS SQL Server 2016 DB from Pega 7
- Access Pega 7 PostgreSQL DB with pgAdmin
1 Pega 7 Setup Used in this Example
- Pega 7.1.6 was used as a Personal Virtual Server (PVS) installation with the VMWare Player as per the Downloading the Exercise System section of the SAE I (7.1) and SSA (7.1) courses provided by Pega Academy. 64-bit Windows 7 was used as the host operating system (running the VMWare Player).
- The virtual machine’s network adapter was configured to use NAT (used to share the host’s IP address).
The MySQL Server is installed on the Windows host operating system. The Pega 7 platform is running on the virtual machine. It is therefore necessary to make sure that the two systems can communicate with each other over the network. The host’s IP address can be looked up by running ipconfig
from the Windows command line. In this case the host’s address is 192.168.0.3
.
After starting the virtual machine, the command line window of the VMWare Player can be used to lookup the IP address of the virtual machine and to ping the Windows host machine to confirm network connection. Execute:
ifconfig
– In this case, the virtual machine has the address192.168.161.129
.ping 192.168.0.3
to confirm that the Pega 7 platform can reach the Windows host system on which the MySQL server will run.
- Also, confirm that the Windows host system can reach the virtual machine by executing
ping 192.168.161.129
from a Windows command line.
2 MySQL DB Server Configuration and Table Used in this Example
For this post, the MySQL Community Server is used to host a database that represents a product catalog. The database has one table called product_catalog
and it has the following five columns:
id
– int(11)
– unsigned, primary key, not nullable, auto-incrementname
– varchar(45)
– not nullablecategory
– enum('electronics', 'books', 'hardware')
– not nullableunit_price
– decimal(6, 2)
– not nullabledescription
– varchar(45)
– nullable- The MySQL database server is installed to run as a Windows service. To access the server from the MySQL Workbench, the following connection parameters are used:
host name
: localhost
– (MySQL Workbench is running on the same Windows host machine)port
: 3306
– (default MySQL server port)username
: root
– not nullablepassword
: *****
– root password was set during MySQL server installation- The MySQL Workbench can be used to create the table and to insert records. For this post, the following 9 rows were inserted into the
product_catalog
table with the MySQL Workbench.
- In order to connect from Pega 7 PRPC to the MySQL server, it is highly recommended to create a dedicated user. The MySQL Workbench or the command line can be used for this task.
- In the MySQL Workbench, click on Users and Privileges and then on the Add Account button.
- Complete the form and specify the username and password.
- Keep the default values for Authentication Type (Standard) and Limit to Hosts Matching (%).
- Navigate to the Schema Privileges tab and click on the button New Entry. In the dialog window, select the correct schema from the dropdown.
- Alternatively, All Schema (%) can be used to give the user access to all schemas. Click OK to close the Schema Privilege Definition dialog.
- The Schema Privileges tab should show the new entry and clicking on the entry allows to set specific Object Rights such as executing
SELECT
,INSERT
andUPDATE
.
- For the example in this post, only
SELECT
is required. Click on the Apply button to save the changes.
3 Configuration of Connection to MySQL Server in Pega 7
As a prerequisite for running the Connector and Metadata Accelerator, a Database Instance needs to be created in PRPC. The Database instance will contain the JDBC connection information necessary for PRPC to establish a connection. This setup can be divided into 3 steps:
(a) Download the MySQL JDBC Driver
- Download the MySQL Server JDBC driver from the MySQL Download Connector/J website.
- For this example, the JDBC driver file
mysql-connector-java-5.1.38-bin.jar
was used.
(b) Copy the MySQL JDBC Driver to PRPC System
- The MySQL JDBC driver JAR file needs to be copied to the PRPC system. FTP can be used.
- See FTP Connect to Pega 7 System for Private Virtual Server Installations for details.
- Copy the
mysql-connector-java-5.1.38-bin.jar
file to theprweb/WEB-INF/lib
folder. - Here (Pega 7.1.6 installed on VMWare as PDN exercise system), the full path of the folder is:
/usr/share/tomcat7/webapps/prweb/WEB-INF/lib
- It may be required to change the access permissions of the
WEB-INF/lib
directory. - The VMWare console can be used to do that by executing the command:
sudo chmod 777 lib
- Once the JDBC driver has been copied to the
WEB-INF/lib
directory, it should look like this:
(c) Configure PRPC with prconfig.xml
- The
prconfig.xml
file needs to be modified to make the MySQL JDBC driver available to PRPC. - Use the VMWare console to access the file system of the PRPC instance and navigate to:
/usr/share/tomcat7/webapps/prweb/WEB-INF/classes
- This folder contains the
prconfig.xml
file. Change the file permissions by executing the command:
sudo chmod 777 prconfig.xml
- The file needs to be edited to add an entry to specify the MySQL JDBC driver class.
- Open the file in the vi editor with
vi prconfig.xml
and add the line:
<env name="database/drivers" value="com.mysql.jdbc.Driver" />
- …to specify the MySQL JDBC driver class.
(d) Restart Pega PRPC
After modifying the prconfig.xml
file, it is necessary to restart the Pega 7 PRPC application. This can be accomplished by:
- Restarting the Tomcat web server on the virtual machine.
- Or by using the Tomcat admin console to restart the
prweb
application (see Accessing Tomcat Admin Console for Pega 7 Personal Virtual Server Installations). - Or by restarting the virtual machine using the VMWarePlayer.
4 Creating a new Database Instance in Pega 7 for MySQL JDBC Access
Before running the Connector and Metadata Accelerator to configure access to an external MySQL table, it is necessary to create a new Database instance in Pega 7 PRPC that encapsulates the JDBC connection to the external MySQL DB server.
- In the Designer Studio, navigate to Records > SysAdmin > Database.
- In the Instances of Database view, click on the +Create button to start the process of creating a new database instance.
- This will open the Create Database form. Enter a description and a name for the database instance and click on the Create and open button.
- This will save the new database instance and open it in edit mode for further configuration.
- On the Database tab of the database instance, specify
use JDBC URL listed below
in the How To Connect dropdown. - Enter the correct MySQL JDBC URL in the JDBC URL text field. In this case, it is:
jdbc:mysql://192.168.0.3:3306/product_catalog
- Where
192.168.0.3
is the IP address of the Windows host system on which the MySQL server is running andproduct_catalog
is the name of the schema to access. - Specify the username and password of the MySQL user account that was created in step 2. In this case the username is
prpc
.
- Click on the Test Connection button to validate the configuration. A new dialog should show a successful connection status message:
- Note: In case of connection errors, refer to earlier sections on network configuration and MySQL server setup. Below are some possible error scenarios and how to approach fixing them.
ERROR: No Suitable Driver Found
- Make sure that the MySQL JDBC driver file
mysql-connector-java-5.1.38-bin.jar
is copied to the/WEB-INF/lib
folder on the PRPC system and that theprconfig.xml
has been modified as described in section 3.
ERROR: Communications Link Failure
- This error is caused most likely by a network connection problem. Make sure the IP address of the MySQL server host machine is correct and reachable from the Pega 7 PRPC host system.
- In this case Pega 7 is running on a virtual machine using the VMWare player. See section 1 for verifying network connectivity between the virtual machine and the MySQL DB Server host.
ERROR: Host… not Allowed to Connect to MySQL Server
- Refer to section 2 and make sure that the MySQL user account (here
prpc
) used by Pega 7 is configured properly. - Check if it has Limits to host matching set to
%
to allow all remote hosts to connect to the MySQL server instance. Note that, by default, theroot
user does not have remote access.
- After configuring the database instance and successfully testing the connection, save the record and reload the Instances of Database view.
- The new external MySQL database instance should now appear in the list as shown in the screen shot above. Here, the database instance is named
MySQLProductCatalog
.
5 Running the Pega 7 Connector and Metadata Accelerator
- Navigate to Designer Studio > Integration > Connectors > Create Other Integration to open the Pega 7 Connector and Metadata Accelerator.
1. Choose Purpose
- For connecting to an external database, the Purpose must be set to
Generate Connector Rules
and the Metadata Type must be set toSQL
. - Select an appropriate rule set name, ideally an integration rule set, and the rule set version.
- The Base Class should be a class derived from the
Data-
class. - The Connector Activity Class should be a class that is derived from the
Work-
class. - For more information refer to the Pega 7 Help entry titled About generating connector rules.
- Click on the Next button to continue.
2a. Process Metadata – Select Database Table
- On the Select Database Table form, select the database instance that was created in section 4 and specify the Schema Name.
- Here, the schema name is
product_catalog
(see section 2). - Once the schema name is entered, the form will show all tables in an auto-complete control.
- Here, the Table Name is set to
products
(see section 2).
- Click on Next to continue.
2b. Process Metadata – Select Table Columns
- Check-mark the required table columns, in this case all available columns are selected.
- Click on Next to continue.
2c. Process Metadata – Select SQL Operations
- Select the SQL operations that are needed for the specific application use case.
- In this case, all operations are selected and for each one the wizard will create an activity rule.
- Click on Next to continue.
2d. Process Metadata – Select Browse Criteria
- If Browse the database was checked in the previous form, the Select Browse Criteria form appears.
- It is used to select specific columns which PRPC can use to query the external MySQL table.
- Click on Next to continue.
2e. Process Metadata – Map Parameter Values
- The Map Parameter Values form does not have to be changed for this example, it allows to specify the input parameters of the activity rules (browse, update etc.) that will be generated for each SQL operation. These input parameters can be changed easiliy later.
- Click on Next to continue.
3. Review and Save
- The Review and Save form, lists all rules that will be generated and modified when the wizard runs.
- Note that a data class will be created that derives from
Data-
, using the table name that was specified earlier. - In addition, all activity rules that will be created for the selected SQL operations are listed here.
- Click on Finish to complete the setup and to let the wizard generate the required rules.
- A final confirmation screen will be shown once the wizard has completed generating the rules.
- Here,
13
rules were generated, including4
activity rules for browse, insert, update and delete operations and5
property rules for mapping the columns of theproducts
table. - Click on Close to complete the setup process and to close the wizard.
6 Access MySQL DB from Pega 7 Application with an Activity Rule
- The auto-generated
Browseproducts
activity can now be unit tested to ensure that PRPC can successfully retrieve data from the external MySQL products table. - Open the rule and remove unnecessary input parameters. For this example, only browsing by category is required. So, the other input parameters can be deleted from this activity or the required flag can be set to No.
- Note that the wizard created from and to parameters for each table column. Some parameters may not be applicable and can be removed.
- Defining from and to parameters only makes sense for numeric table columns, where a range is provided to query the table (e.g.: search products where unit price is between $0.00 and $100.00).
- Here, the list of input parameters of the
Browseproducts
activity was reduced as shown below.
- Switch to the Steps tab and expand the first step that calls the Obj-Browse method.
- Configure the selected fields as needed and set the CONDITION to either
Value Only
or useIs Equal
for a comparison to an input parameter value.Value Only
means that the field will not be used for querying but that it will be included in the search results. - For testing, remove the comment (
//
) from the step which calls Show-Page, to Show the temporary list page of query results. When the activity is executed, this will result in a popup window showing the SQL query results in XML format.
- Click on Save to commit the changes to the activity.
- The activity can be run by clicking on Actions > Run.
- In the activity execution window, enter the value for the input parameter to query by, in this case the input parameter is category and only table rows where the category matches
electronics
should be returned (seeIs Equal
condition on the steps page of the activity). - Click on the Execute button to run the browse activity.
- A new dialog window will open and show the query results in XML format.
- The XML output will contain the total number of rows retrieved from the table, the actual SQL query string that was executed against the external MySQL table and the result data.
- The node
pxResults
contains the query results. Each entry is of the typePXC-OrderCat-Data-products
. - In this case,
4
products, where the category matcheselectronics
, were retrieved from theproducts
table on the MySQL server.
- The activity rule can now be used in a Pega 7 application as a data source for e.g. populating a drop-down control or a repeating grid on a UI section.
21 thoughts on “How to Access MySQL DB from Pega 7”