Weblogic 12.1.3 opens a transaction even with TransactionAttributeType.NEVER or NOT_SUPPORTED

The problem

An stateless EJB is used to process lot of records, one by one, without using a global transaction that envelopes everything.

Well, here the initial solution is to annotate the EJB with:

@TransactionAttribute(TransactionAttributeType.NEVER)

An example of this kind of class would be:

@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEVER)
public class EJBThatProcessAll {
    @EJB
    private EJBThatProcessRecord ejbThatProcessRecord;

    public List<Result> processRecords(List<Record> records) {
        return records.parallelStream()
                      .map(EJBThatProcessRecord::processRecord)
                      .collect(Collectors.toList());
    }
}

Then the method which process the records calls (for every record) another EJB that is anotated with:

@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

So every record is processed in its own transaction.

@Stateless
@LocalBean
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class EJBThatProcessRecord {
    public Result processRecord(Record record) {
        return doSomethingWithRecord(record);
    }
}

Issues found

Even every call to process a record was less than 1 sec. at some moment I always got a TransactionTimeoutException. The container was complaining that some transaction lasted more than 60 s (which was the timeout set for this application).

Attempt 1

Well, I tried several things. The first one was changing TransactionAttriibuteType.NEVER by TransactionAttriibuteType.NOT_SUPPORTED. Failed with same error.

Attempt 2

Since I was not sure about the parallel processing, I tried to change parallelStream() to stream(). Failed with same error.

Attempt 3

A mate read somewhere that if the EJB was asynchronous, it could avoid the transaction, so I annotated the EJB with:

@Asynchronous

And changed the method to return a Future<List<Result>>. But the result was the same. Failed.

Good solution

Finally I changed the main EJB so the transactions were bean managed:

@TransactionManagement(TransactionManagementType.BEAN)

And it worked!. No need anything else but a good comment above the annotation about why I did it, because I know that sooner or later another developer will find it weird and would try to change it by the initial solution.

Testing

At the beginning the initial solution was tested through JUnit and it worked fine. I found the bug while testing the actual web application, so it’s clear that it’s the container that creates a transaction when the EJBThatProcessAll is called, even when it’s annotated to not use transactions at all. And it this case the application container was Weblogic 12.1.3 I don’t know what happens in other containers like JBoss.

Bootstrap keyboard navigable dropdown list with AngularJS

This time I comes with a problem I’ve faced in a project I’m currently working. We have some groups of Bootstrap components that mix a button with a HTML unordered list (button + ul tags).

The problem was that the user was not able to use the keyboard (cursor keys) to navigate through the elements of the list, so for a big form like the ones we have, changing keyboard to mouse is annoying for the users. They must fill lot of information and is always faster to use the keyboard.

8767790

So we had something like this (code simplified and cleaned to remove own CSS classes and other not related stuff):

<div class="btn-group">
    <button type="button" class="btn btn-default dropdown-toggle btn-block" data-toggle="dropdown">
        <span><fmt:message key="generic.no.selection" /></span>
    </button>
    <ul class="dropdown-menu">
        <li ng-repeat="element in elements.getPossibleValues()">
            <a ng-click="doSomething()">element.code</a>
        </li>
    </ul>
</div>

There are several solutions to solve this situation. I started trying to solve it directly with JQuery using a ngController function that was triggered when the user clicked (with mouse or keyboard) the button. Then I tried to do it more generic and cleaner, so I implemented an AngularJS directive, so now other developers only need to add an attribute to the button tag:

/**
 * Binds an on click function that sets the focus on the first element of the dropdown next to
 * the element that has the attribute 'focus-on-first-dropdown-element'. 
 */
uiModule.directive("focusOnFirstDropdownElement", function() {
    return {
        restrict: 'A',
        link: function (scope, element) {
	    element.bind('click', 
            function() {
                // Selects the sibling UL that contains the dropdown elements
                var list = element.next();
                // Put the focus on the first LI (a) element after some ms. The timeout is needed to give time to
                // show the list. If the timeout is not setup, the focus goes to the input text box associated to the button
                setTimeout(function() {
                    list.find('a:first').focus();
                }, 100);
            });
        }
    };
});

Also some modifications to the HTML code must be done:

  1. HTML <ul> tag needs to have the attribute role=”menu” (see Bootstrap).
  2. HTML <a> tags within the <li> elements must have an href attribute.
  3. Button tag must use the directive focusOnFirstDropdownElement.

So the new HTML after the changes is:

<div class="btn-group">
    <button type="button" focus-on-first-dropdown-element class="btn btn-default dropdown-toggle btn-block" data-toggle="dropdown">
        <span><fmt:message key="generic.no.selection" /></span>
    </button>
    <ul class="dropdown-menu" role="menu">
        <li ng-repeat="element in elements.getPossibleValues()">
            <a href="" ng-click="doSomething()">element.code</a>
        </li>
    </ul>
</div>

After that, now the user is able to use the keyboard to use the dropdown list.

Enjoy!

Technical eBooks & videos sales at PacktPub

From now until 6th of January 2015, my friends of PacktPub are selling eBooks & videos for $5 or 4.7€. I think Xmas dates are a good time for purchasing some books and learning something new during the holidays.

PacktPub Xmas sales

As you might know I’ve read some of their books, and although they aren’t best sellers, most of them are good for introduce people to new frameworks, tools and theories, so they are interesting enough to give them an opportunity, and are very cheap, so why not?

I think this time I’ll try some eBook about Big data, arduino or Angular JS

Well, now it’s time to relax, I wish you all Merry Xmas and a very, very happy New Year 2015.

Java OutOfMemoryError: unable to create new native thread

Another strange problem raised last week at work that finally I’ve solved. To be honest, the problem is not so strange, but the way it showed up and the errors we got were. As most of the problems, when you discover the roots or the reasons, they stop being rare and you think – “It’s obvious!”🙂.

Background

I like to be concise when explaining this kind of things, so to sum up, we have a Java server that acts as an authenticator for other applications. This authentication server uses the javax.security.auth.login Java package to connect and authenticate through an LDAP server.

Besides, an intesive process tries to get information from another application through a web service that needs authentication. To get authenticated, this process uses the authentication server described above but after 300 hundred calls or so in one or two minutes we start seeing this Java exception stacktrace:

04:13:07.682 [] ERROR com.xxxxx.xxxxx.server.services.XXXXXXXXServiceHelper.login(): 
Authentication error : java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:691)
at com.sun.jndi.ldap.Connection.(Connection.java:231)
at com.sun.jndi.ldap.LdapClient.(LdapClient.java:136)
at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1600)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2698)
at com.sun.jndi.ldap.LdapCtx.(LdapCtx.java:316)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:211)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:154)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:84)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.ldap.InitialLdapContext.(InitialLdapContext.java:153)
at com.xxxxx.xxxxx.server.ldap.LdapConnection.open(LdapConnection.java:115)
at com.xxxxx.xxxxx.server.ldap.LdapConnection.open(LdapConnection.java:97)
at com.xxxxx.xxxxx.server.ldap.LdapLoginModule.attemptAuthentication(LdapLoginModule.java:325)
at com.xxxxx.xxxxx.server.ldap.LdapLoginModule.login(LdapLoginModule.java:175)
at sun.reflect.GeneratedMethodAccessor10194.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.security.auth.login.LoginContext.invoke(LoginContext.java:784)
at javax.security.auth.login.LoginContext.access$000(LoginContext.java:203)
at javax.security.auth.login.LoginContext$4.run(LoginContext.java:698)
at javax.security.auth.login.LoginContext$4.run(LoginContext.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.login.LoginContext.invokePriv(LoginContext.java:695)
at javax.security.auth.login.LoginContext.login(LoginContext.java:594)

At the beginning people thought that our authentication server application was running out of heap memory… but it doesn’t, because it didn’t stop working. After 3 or 4 minutes it worked fine and no errors were shown until the next execution of the intensive process. Then, one of the development teams involved in this issue called me to help them investigating the problem.

I was sure it wasn’t a problem of the heap memory of the authentication server because the OutOfMemoryError was shown in a java log trace of the application, not the application server where it is deployed, so the application worked… not really fine, but worked!. Another possible cause I thought was that the error source was LDAP, but our LDAP is not Java based, either the log files gave any error.

The key

The key to this problem was in front of our eyes, the message associated to the OutOfMemoryError: unable to create new native thread

The solution

After reading a bit on several pages, it’s possible that this problem could be solved using different solutions. But it’s sure it can’t be solved increasing the heap size of the JVM memory, because the JVM heap memory was fine.

I’m not a Linux system administration, but after investigating a bit, I discovered that it was the machine, where the application server with our authentication server was installed, which was running out of resources for creating system operating threads. Our system administrator realized that the Linux machine had a default configuration, so the running processes limit and the file descriptors limit was too low. Finally I asked the system administrator to increase those numbers. It can be done modifying the values for nofile and nproc from the file /etc/security/limits.conf:

#        - nofile - max number of open files
#        - nproc - max number of processes

The safest way would be limiting the values only for the user involved, not the whole machine, but that’s must be a system administration decision.

References

Android – GoogleAuthUtil.getToken Exception error: Unknown

There are a lot of guides out there to do OAuth2 authentication through Google on an Android application. I’ve been reading several of them, javadocs, Stackoverflow questions and so on, trust me. Finally, after got connected to Google I wanted to retrieve the token for the user connected. For that I used the GoogleAuthUtil class. Here is a snippet of the code I’ve written to get the token:


    /**
     * Request code for auto Google Play Services error resolution.
     */
    private static final int REQUEST_CODE_RESOLUTION = 89898;

    /**
     * User connected token.
     */
    private String mToken;

    /**
     * Task for retrieving the token of the connected user
     */
    private class GetTokenTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... userAccount) {
            String token = null;
            try {
                Log.d(TAG, "Retrieving token for [" + userAccount[0] + "]");
                token = GoogleAuthUtil.getToken(getActivity(), userAccount[0], Scopes.PROFILE, new Bundle());
            } catch (UserRecoverableAuthException e) {
                Log.w(TAG, "Error retrieving the token: " + e.getMessage());
                Log.d(TAG, "Trying to solve the problem...");
                startActivityForResult(e.getIntent(), REQUEST_CODE_RESOLUTION);
            } catch (IOException e) {
                Log.e(TAG, "Unrecoverable I/O exception: " + e.getMessage(), e);
            } catch (GoogleAuthException e) {
                Log.e(TAG, "Unrecoverable authentication exception: " + e.getMessage(), e);
            }
            return token;
        }

        @Override
        protected void onPostExecute(String token) {
            if (token != null) {
                mToken = token;
                Log.d(TAG, "We have a token!: " + token);
            } else {
                Log.d(TAG, "Can't retrieve any token");
            }
        }
    }

But always got the same error:

09-05 13:07:19.109  23577-25973/com.xxxxx E/Login﹕ Unrecoverable authentication exception: Unknown
    com.google.android.gms.auth.GoogleAuthException: Unknown
            at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
            at com.xxxxx.views.LoginFragment$GetTokenTask.doInBackground(LoginFragment.java:232)
            at com.xxxxx.views.LoginFragment$GetTokenTask.doInBackground(LoginFragment.java:225)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

The error is all but descriptive. After browsing lot of pages I discovered the trick, the scope was wrong!. Well, the scope seems right, but it lacks a string header with the value ‘oauth2:’:

final String scope = "oauth2:" + Scopes.PROFILE;
token = GoogleAuthUtil.getToken(getActivity(), userAccount[0], scope, new Bundle());

After that, everything went right. Hope this helps!

Book review: Three.js Essentials

This time I’m going to write about another little book I’ve been reading last weeks, Three.js: Essentials.

ThreeJS Frontcover

The book is an introduction or a wide getting starting guide to several features of Three.js Javascript library. Three.js is a high level WebGL wrapper, that tries to make easier to work with WebGL on the browser, so you will be able to render 2D/3D objects using Javascript/HTML5.

The book is aimed to people who want to learn a bit about Three.js. The author says you must know Javascript but not WebGL, maths or 3D rendering. I agree with him about you don’t need to know WebGL or 3D rendering, but you should have knowledge about geometry at least, if not, some of the concepts of the book will be hard to understand. It’s obvious to me that if you’re interested in a 3D library means that you know a bit of geometry (x-y-z axies, vectors, matrix algebra, planes, etc.). If you don’t you can follow the book and the samples, but as I’ve already said, it’ll be harder.

Three.js: Essentials is like a big tutorial where you will find lot of code examples of everything explained along the book. The code is very easy to read and modify, so you’ll be able to do experiments and learn how Three.js works. The book has seven chapters where you’ll learn how to draw 3D objects, animate them, do collision detection, apply light, materials and textures, make 3D random mazes, control objects with mouse or keyboard, generate particle systems, combine HTML5 with Three.js or learning how to use Blender along Three.js.

If you want to start working with 3D in the browser this book is really helpful, but you’ll need to combine it with a complete reference of Three.js, because as the author says, there are a lot of features that are not covered and will give you lots of possibilities to do interesting things.

The good
Really helpful introduction to Three.js. Lot of easy to follow code samples.
Not too long and easy to read.

The bad
Several complex concepts (mesh, textures, materials, matrix algebra, etc.) can be hard to understand for people new to 3D.

Conclusion
If you want to learn about Three.js and already know a bit about 3D buy this book because it teachs a lot of concepts of the library and is a cheap book.

Book review: Redmine Plugin Extension and Development

Wow, a while ago since I don’t write here… It could seems that I haven’t facing any problems up to date. Although I did, I’ve thought they weren’t too interesting to write about.

I’m just arrived from my month of holidays where apart of playing with my daughter and laying down in the beach I’ve been reading an interesting book: Redmine Plugin Extension and Development. I’m the creator of Redmine SpentTime plugin for Redmine along other private plugins I’ve written for the company I work at, so I know a bit about Redmine’s plugins.

redmine_plugins_book_cover

The book is very easy and fast to read, with a plain and direct language that tries to explain every aspect of Redmine’s plugin development. The book is short, so you shouldn’t expect most of it, so don’t expect learning anthing about Redmine or Ruby on Rails because it isn’t the goal of the book. It seems obvious but this is not a book for novices on Ruby on Rails applications, you must know how this apps works to develop extensions for Redmine because you’ll have to know how Redmine works as well.

The good things of the book
There are several good things in this book, first of all the direct and plain language. The book is thought as a large tutorial from the beginning to the end. You could do a new plugin step by step. And it explains the most important parts of Redmine’s plugins as hooks, permissions & security or how to do new models searchables. The explanation of hooks is the most important part of the book, very worthwhile to read.

Also I like the permissions & security section and the importance the author gives to the testing side of the development. In this last section you’ll find valuables ideas about testing and continuous integration (CI) tools like Travis.

The bad parts of the book
Also this book have some bad things… although not too bad. First of all it tries to explain the development of plugins using as example another plugin (KnowledgeBase), but you must refer to GitHub repository to see the code examples, so you need a browser if you want to see the code. That is not very important for all people, but I think sometimes is good to have the code in the book so you don’t need to browse any page.

Another missing aspect about plugins is the internazionalization. The book covers basic aspects of the development like Bundle that are common to every Ruby on Rails application, so treating things as internazionalization is a must too… I know it because most of the contributions to my public plugin has been translations… although it’s very easy to do, it should have been covered in the book.

Conclusion
Redmine Plugin Extension and Development is a very good book for everyone that wants to write extensions for Redmine. After reading this book I planned some changes on the plugins I’ve wrote because I’ve learned very interesting things from it. You can read it in less than a week in your spare time, so it isn’t a lost time.