Introduction
Understanding how the WMB JDBC is critical to handle the connectivity with database. In this article, I have tried to explain the basis concept about JDBC in WMB and how it should be configured in different scenario.
Understanding JDBC Connectivity
1. Setting up a JDBC provider for type 4 connections
Use the mqsi crea teco nfig urab lese rvic e or the mqsi chan gepr oper ties command to configure a JDBC provider service.
This is an example of JDBC provider properties.
JDBCProviders: DBName
Properties:
| |
|
Run the mqsirepo rtpr oper ties comm and to view the list of available JDBCProvider services.Substitute the name of your broker in place of broker_name.
mqsi repo rtpr oper tie sbro ker_ nam e -c JDBCProviders -a -o AllR epor tabl eEnt ityN ame s
command to display the supplied Oracle definition:
mqsi repo rtpr oper tie sbro ker_ nam e -c JDBCProviders -o Oracle -r
A JDBCProvider service has the following properties:
conn ecti onUr lFor ma t:
jdbc :db2 ://[ serv erNa me]: [por tNum ber] /[da taba seNa me]: user =[us er]; pass word =[pa sswo rd] ;
Example of creating JDBC provider with required parameters :
mqsi crea teco nfig urab lese rvic e BRKName -c JDBCProviders -o Instance1 -n data base Type ,con nect ionU rlFo rmat ,con nect ionU rlFo rmat Attr 1,da taba seNa me,d escr ipti on,j arsU RL,p ortN umbe r,se rver Name ,typ e4Da taso urce Clas sNam e,ty pe4D rive rCla ssNa me -v "Ora cle, jdbc :ora cle: thin :use rnam e/pa sswo rd@h ostn ame: 1526 :Ins tanc e1,I nsta nce 1,In stan ce 1,Si mpli fied Database Routing Sample Data base ,/ho me/username/ jdbc /lib ,152 6,ho stna me,o racl e.jd bc.x a.cl ient .Ora cleX ADat aSou rce, orac le.j dbc. Orac leDr iver "
2. Securing a JDBC type 4 connection
Set up security for the JDBC connection if required by the database provider otherwise you can pass the credentials in con nect ionU rlFo rmat .
- Identify the user ID that you want to associate with the JDBCconnection, or create a user ID with a password, following the appropriate instructions for your operating system and database.
- Run the mqsi
setd bpar msco mman d to associate the user ID and password with the security identity that is associated with the database that you will access using the JDBCProvider configurable service.
The following values and order of preference are used by the broker for the JDBC connection:
- The user ID and password that you have set for the specific database, by using the mqsisetdbparms and specifying the database in the -n parameter.
Use the following command format:
mqsisetdbparms broker_name -n security_identity -u userID -p password
3. Interacting with databases by using the Java Comp ute nod e
The broker supports type 4 drivers, but does not supply them means whatever database you are using, You must obtain these drivers from those database vendor.
Use the broker JDBC Provider to benefit from the following advantages:
- Use broker configuration facilities to define the connection, and to provide optional security, in preference to coding these actions.
- Configure the broker and the databases to coordinate access and updates with other resources that you access from your message flows, except when the broker is running on.
- Use the broker Java API
getJ DBCT ype4 Conn ecti on t o initiate the connection, and then perform SQL operations by using the standard JDBC APIs. The broker manages the connections, thread affinity, connection pooling, and life cycle in background so you dont need to worry about for all these handling. - By default connection pooling is zero(0) and you can configure it according to your database.
- If a connection is idle for approximately 1 minute, or if the message flow completes, the broker closes the connection.
When you have configured the JDBCProvider, you can establish a JDBC type 4 connection to the database by using th e ge tJDB CTyp e4Co nnec tio n ca ll on th e Mb Nod e in terf ace .
The following code provides an example of its use:
public class MyJavaCompute extends MbJavaComputeNode {
public void evaluate(MbMessageAssembly inAssembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbMessage inMessage = inAssembly.getMessage();
// create new message
MbMessage outMessage = new MbMessage(inMessage);
MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly,outMessage);
try {
// Obtain a java.sql.Connection using a JDBC Type4 datasource - in this example for a
// JDBC broker configurable service called "MyDB2"
Connection conn = getJDBCType4Connection("MyDB2",
JDBC_TransactionType.MB_TRANSACTION_AUTO);
// Example of using the Connection to create a java.sql.Statement
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet srs0 = stmt.executeQuery("SELECT NAME, CITY FROM MySchema.MyTable");
stmt.executeUpdate("UPDATE MySchema.MyTable SET CITY = \"Springfield\" WHERE Name = \"Bart\"");
.
// Perform other database updates
.
} catch (SQLException sqx ){
sqx.printStackTrace();
} finally {
// Clear the outMessage
outMessage.clearMessage();
}
}
}
0 comments:
Post a Comment