Thursday, December 22, 2011

How to setup and use DB2Mon

I used to use TOAD to monitor the connection to Oracle, but when I moving to DB2 world, I am struggling to find out a similar tool to look at what kind connections/activities happen on the Database. One of my collegue suggested DB2Mon, I set off and decide to give it a try, here I record what I need to do:
1. Start IBM DB2 Configuration Assistant, right click, Add Database Using Wizard, Manually configure a connection to a database; setup your database connection; Test your connection by right click the newly created connection->TestConnection->CLI
2. Start your DB2Mon, you should see your newly created Database connection, select it and input your user/password.

Thursday, December 08, 2011

How to get a JDBC connection in EJB 3 running on WebSphere

There are several ways to get a JDBC connection in EJB3 running on WebSphere:
1. JNDI lookup
2. @Resource Injection
2.1 In ibm-ejb-jar-bnd.xml
<session name="OrderRepositoryBean">
<resource-ref name="DFSAdabasDataSource" binding-name="jdbc/DFS_ADABAS" />
</session>
2.2 In OrderRespositoryBean
@Resource(name = "DFSAdabasDataSource")
private DataSource ds;
3. NOT WORK!!! @Resource Injection by using mappedName, assuming the JNDI name is : jdbc/DFS_ADABAS
// In OrderRepositoryBean
@Resource(mappedName = "jdbc/DFS_ADABAS")
private DataSource ds;
4. Using OpenJPAEntityManager
@PersistenceContext(unitName = "CACSAMP")
private EntityManager emDFS;
......
(Connection) ((OpenJPAEntityManager) emDFS.getDelegate()).getConnection();

Wednesday, December 07, 2011

Configure IBM InfoSphere Classic JDBC Driver datasource in WebSphere

To setup the InfoSphere Classic JDBC Data source in WebSphere, you could follow the following steps:
1. Create new JDBC Provider
1.1 Resources->JDBC->JDBC providers->new
1.2 Database type: User-defined; Implementation class name: com.cac.jdbc.ConnectionPoolDataSource; Name: IBM InfoSphere Classic JDBC
1.3 Class path: ${User-defined_JDBC_DRIVER_PATH}/DataFederationServer/cacjdbc21.jar
1.4 Review summary->Finish->save
2. Create Java 2 Connector authentication data entry
2.1 Security->Global security; Under Authentication cache settings, click Java Authentication And Authorization Service->J2C authnetication data; new
2.2 Alias: ; UserID: ; Password: ; Apply-> save
3. Create new Data Source
3.1 Resources->JDBC->Data sources; new
3.2 Data source name: DFS_XXXX; JNDI name: jdbc/DFS_ADABAS
3.3 Select the JDBC provider created in step 1.
3.4 Data store helper class name: com.ibm.websphere.rsadapter.GenericDataStoreHelper; Uncheck used for CMP check box.
3.5 Component-managed authentication alias: the one you created in step 2.

3.6 Review summary->Finish->save
3.7 setup the Custom properties for the newly create Data source by click on the Data source name -> Customer properties
3.7.1 URL: your Data Federation Server JDBC URL;
3.7.2
webSphereDefaultIsolationLevel: 1 (READ_UNCOMMITED); very tricky, I can only make it work by setting to 1. It turns out the backend (ADABAS) is set to support READ_UNCOMMITED.
3.8 If you run into "JDBC hung when try to close connection" in your application, try to turn off the statement cache; Search IBM fix pack change list for similar problem for Oracle, and Sybase JDBC connection.
You almost done, just restart your web sphere server and start to use the JDBC Data source.

How to currentSchema for DB2 DataSource in WebSphere

Without setting the currentSchema for DB2, you need to fully qualified the DB object (table, view etc) name.
According IBM documentation, currentSchema means "This allows the end user or application to name SQL objects without having to qualify them by schema name."

How to setup the crrentSchema for DB2 DataSource in Websphere?
Resources->JDBC->Click your Data Source name->Customer properties->currentSchema.
Restart your Websphere to make it take effective.
BTW: The DataSource currentSchema setting could be co-exist with the openjpa.jdbc.Schema setting in persistent.xml without intereference with each other.

Friday, November 18, 2011

How to get the Database connection URL from OpenJPA EJB

Normally you would like to have a quick and easy way to tell which DB your application is connect to. To get this kind of you information in an OpenJPA/EJB3 stack, you may be able to do the following:
public String getDataBaseURL() {
        final Connection conn = (Connection) ((OpenJPAEntityManager) em.getDelegate()).getConnection();
        String url;
        try {
            url = conn.getMetaData().getURL();
        } catch (final SQLException e) {
            url = "Failed to get the DB URL";
        }
        return url;
    }

Tuesday, September 13, 2011

Windows firewall blocks access to Apache Tomcat service from remote computer

Today, my colleague installed Tomcat as windows service on a Windows 7 box, strangely he could access via http://127.0.0.1:8080 and http://ipofserver:8080, but he could not access http://ipofserver:8080 from a remote computer. After some digging, it turns out to be the Windows 7 firewall blocked the remote access traffic. The solution of the problem is simple: just manually start the tomcat7.exe, Windows 7 will show the firewall warning dialog, just allow access.;-)

Tuesday, August 30, 2011

JNI Refreshment

Today one of my colleague asked me some questions on JNI implementation. The knowledge seems fading away from me. To avoid it happens again, I recorded the basic steps:
  1. Delare your native methods in a normal Java class, for example, HelloJNI.java.
  2. javac the java file(HelloJNI.java).
  3. javah -jni HelloJNI to generate the .h file(s).
  4. Write your native code, using the .h file generated in step 3; copy the function prototye from the .h file into your .c file.
  5. compile by using Visual Studio 2005 command shell(cl command).
  6. Modify your java class to load the library; recompile it; ready to run it!!!
Reference:
  1. http://java.sun.com/docs/books/jni/html/jniTOC.html
  2. http://patriot.net/~tvalesky/jninative.html
  3. http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jniexamp.html#comp
  4. command to compile your c file: c:\test\jnitest>cl /I"C:\Program Files (x86)\Java\jdk1.6.0_24\include" /I"C:\Program Files (x86)\Java\jdk1.6.0_24\include\win32" -LD HelloJNI.c -Felibnative.dll
  5. JNI in package

Thursday, July 07, 2011

Decoding IBM DB2 error message

Ever get an IBM DB2 error message like "SQL0407N Assignment of a NULL value to a NOT NULL column TBSPACEID=n1, TABLEID=n2, COLNO=n3 is not allowed. What actually does "TBSPACEID=n1, TABLEID=n2, COLNO=n3" mean? Don't ask me, you should ask IBM DB2, but how?

SELECT C.TABSCHEMA, C.TABNAME,
C.COLNAME
FROM SYSCAT.TABLES AS T,
SYSCAT.COLUMNS AS C
WHERE T.TBSPACEID = n1
AND T.TABLEID = n2
AND C.COLNO = n3
AND C.TABSCHEMA = T.TABSCHEMA
AND C.TABNAME = T.TABNAME

Wednesday, January 05, 2011

Install Oracle Developer Suite 10g on Windows 7 (64bit)

Recently, I got a task to install Oracle Developer Suite on Windows 7(64bit), but there seems lots of hassles to achieve this goal. There is a thread on Oracle forum on that topic. Here I log what I did to get it installed on my Windows 7(64bit) with 8G RAM.
  • extract the setup files (version 101202) downloaded from otn.
  • right click the install\setup.exe property, set compatibility to Windows XP SP2
  • edit the virtual memory size to custom size from 1024m to 8000m, restart it
  • run the install\setup.exe
Hope this helps.