Translate

miércoles, 18 de enero de 2017

Jboss datasources: how to set autocommit to false by default with example

Como cambiar el valor de autocommit  en los data sources de JBOSS  EAP 6.4 con ejemplo práctico:

In this post i´m going to resolve a problem with the jboss datasources, when you open a connection and get it throught a jboss datasource the autocommit mode is setted to true by default.

When you develop a new application where you can control all your code it is not a problem, but when you have  inherited code that you cant change it, for example in a migration from a weblogic to a jboss server it could be a real problem


In my case I had java code inherited from weblogic and in some place of the code i was suffering problems with the autocommit state of my jdbc connection. I could not do commit or rollback becasuse the autocommit was setted to true, and it throw the next exception

java.slq.SQLException: you cannot rollback with autocommit set!

This is because in weblogic 10.3 the jdbc connections have a extrange behavior because if you try to do a rollback with autocommit = true it has no effect butit doesn´t thow an exception. When you migrate this code to Jboss is a real problem that you have to handle.

In order to find a solution I have debugged the inherited code to looking for the classes that has been used to get the datasource connection. I found that the datasource class is WrapperDataSource 



In my case the solution was to create a Wrapper of the class WrapperDataSource in order to add the code to setAutoCommit(false) in the  getConnetion methods, in order to get the connection with autocommit setted to false by default, when the application instace a jdbc connection from Jboss server.


To do this we need to create a java project with the sources and the compiled classes of the this jar: ironjacamar-jdbc-1.0.31.Final-redhat-1.jar in order to modify and compile the WrapperDataSource class. This is the class that Jboss uses to create a new JDBC connection.

You can find this sources in the nex  link or in my github repository in the lib folder.

https://maven.repository.redhat.com/techpreview/all/org/jboss/ironjacamar/ironjacamar-jdbc/1.0.31.Final-redhat-1/

Also we need some jars in order to compile the WrapperDataSource class, the jars are those:

  • ironjacamar-core-api-1.0.31.Final-redhat-1.jar
  • ironjacamar-jdbc-1.0.31.Final-redhat-1.jar
  • jboss-connector-api_1.6_spec-1.0.1.Final-redhat-3.jar
  • jboss-logging-3.1.4.GA-redhat-2.jar
  • jboss-transaction-api_1.1_spec-1.0.1.Final-redhat-3.jar


All jars are in the Jbos EAP 6.4 distribution or in my github repository code too.

https://developers.redhat.com/products/eap/download/



Now in the java project with all the jars (added to the poject build path) and the source files we can make the modification that we need. We only need to create the package org.jboss.jca.adapters.jdbc and copy the class WrapperDataSource.java from the source files into it. If you need to modify more classes take it from the source code, copy in the project and modify it.



Next step is to look up the getConnection Methods and add the setAutocommit(false), after the connection creation,


Be carefull because in this version exists two getConnection methods and i have to modify both.

In next step you have to compile this class and mix with the original classes and create the new jar with my changes. In order to do this i have create an ant script (buidl.xml) into the ant folder which unzip the orginal, jar, compile the modified class and add to the new jar.

--

<?xml version="1.0"?>

<project name="WrapperDataSource" default="build" basedir=".">

 <property file="build.properties"/>
 
 
 <target name="clean" description="Deletes compiled and generated code">
         <delete dir="${build.dir}" />
 </target>
 
 <target name="build" depends="clean" description="build Wrapper">
  <mkdir dir="${build.dir}" />
  <unzip src="${build.dir}/../lib/ironjacamar-jdbc-1.0.31.Final-redhat-1.jar" dest="${build.dir}"/>
  <path id="lib.path.ref">
      <fileset dir="${lib.path.ref}" includes="*.jar"/>
  </path> 
  <javac srcdir="${source.dir}" destdir="${build.dir}" classpathref="lib.path.ref" target="1.6" 
 source="1.6" debug="true"/> <jar   destfile="${jar.dir}/${jar.name}"
         basedir="${build.dir}"         
  />
 </target></project>
--

To build the JAR with the new modified class, be sure to have Apache Ant installed, go to ant directory and execute the "ant" command.

The result shoud be:


The result is in the target/dist foder where you can find the new MyJbossWrapperDataSource.jar



Finally the last step consists in change the original ironjacamar-jdbc-1.0.31.Final-redhat-1.jar by the new MyJbossWrapperDataSource.jar modifiying the Jboss libray module.

To do this action you have to go to your JBOSS EAP6 installation and locate the next path:

[JBOSS_HOME]\modules\system\layers\base\org\jboss\ironjacamar\jdbcadapters\main\

Copy the new MyJbossWrapperDataSource.jar to this location



and modify the module.xml commenting the old jar file and adding the new MyJbossWrapperDataSource.jar

--
<module xmlns="urn:jboss:module:1.1" name="org.jboss.ironjacamar.jdbcadapters">
    <properties>
        <property name="jboss.api" value="private"/>
    </properties>

    <resources>
        <!--resource-root path="ironjacamar-jdbc-1.0.31.Final-redhat-1.jar"/>-->
        <resource-root path="MyJbossWrapperDataSource.jar"/>
        <!-- Insert resources here -->
    </resources>

    <dependencies>
        <module name="javax.api"/>
        <module name="javax.resource.api"/>
        <module name="javax.validation.api"/>
        <module name="org.hibernate.validator"/>
        <module name="org.jboss.as.naming"/>
        <module name="org.jboss.as.transactions"/>
        <module name="org.jboss.common-core"/>
        <module name="org.jboss.jboss-transaction-spi"/>
        <module name="org.jboss.ironjacamar.api"/>
        <module name="org.jboss.logging"/>
        <module name="org.jboss.threads"/>
        <module name="javax.xml.stream.api"/>
    </dependencies>
</module>

--

You can download my eclipse project from github in the next link, if you want to get the code with this change appied, or make more changes in your jboss datasource.

https://github.com/antuansoft/MyJbossWrapperDataSource

Happy Wrapping!!!


24 comentarios:

  1. Este comentario ha sido eliminado por el autor.

    ResponderEliminar
  2. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    best rpa training in bangalore
    rpa training in bangalore | rpa course in bangalore
    RPA training in bangalore
    rpa training in chennai
    rpa online training

    ResponderEliminar
  3. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ResponderEliminar
  4. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    AWS Training in Bangalore

    AWS Training in pune

    ResponderEliminar


  5. It should be noted that whilst ordering papers for sale at paper writing service, you can get unkind attitude. In case you feel that the bureau is trying to cheat you, don't buy term paper from it.
    360digitmg big data analytics course malaysia

    ResponderEliminar
  6. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!

    data science training

    ResponderEliminar
  7. Such a very useful Blog. Very interesting to read this article. I have learn some new information.thanks for sharing. Click here for data science course in pune with placements

    ResponderEliminar
  8. Wonderful thanks for sharing an amazing idea. keep it...

    Became an Expert In Google Cloud Platform Training in Bangalore! Learn from experienced Trainers and get the knowledge to crack a coding interview, @Softgen Infotech Located in BTM Layout.

    ResponderEliminar
  9. Great post!I am actually getting ready to across this information,i am very happy to this commands.Also great blog here with all of the valuable information you have.Well done,its a great knowledge. Real Time Experts training center bangalore


    ResponderEliminar
  10. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site. Real Time Experts Training in Bangalore center address bangalore


    ResponderEliminar
  11. This is one of the high-quality assets I even have located in pretty a while.
    Once Again Thanks for Sharing this Valuable Information i love this i Can Share this with My Friend Circle.

    click here formore info.
    ............................................................

    ResponderEliminar
  12. This is one of the high-quality assets I even have located in pretty a while.
    Once Again Thanks for Sharing this Valuable Information i love this i Can Share this with My Friend Circle.

    click here formore info.
    ............................................................

    ResponderEliminar
  13. Great post i must say and thanks for the information.
    Data Science Course in Hyderabad

    ResponderEliminar
  14. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.
    AWS training in Chennai

    AWS Online Training in Chennai

    AWS training in Bangalore

    AWS training in Hyderabad

    AWS training in Coimbatore

    AWS training

    ResponderEliminar
  15. I am sure that this is going to help a lot of individuals. Keep up the good work. It is highly convincing and I enjoyed going through the entire blog.
    data scientist training in hyderabad

    ResponderEliminar
  16. Ideas from Capcom normally end up as great products, and idm serial number is a great example.








    ResponderEliminar
  17. You completed certain reliable points there. I did a search on the subject and found nearly all people will agree with your blog.
    data analytics course in hyderabad

    ResponderEliminar
  18. This was really one of my favorite website. Please keep on posting. data science training in kanpur

    ResponderEliminar
  19. I will truly value the essayist's decision for picking this magnificent article fitting to my matter.Here is a profound depiction about the article matter which helped me more.
    best data science training in hyderabad

    ResponderEliminar
  20. Learn many things from your blog, great work, keep shining and if you are intresting in data engineering then checkout my blog data science classe in satara

    ResponderEliminar