Software Transactional Memory Example using Multiverse

Software Transactional Memory Example using Multiverse

Transactions are an essential part of any business critical application as these ensure the integrity of the data being managed by the same application. Transactions ensure that the data remains in a consistent and integral state after this is manipulated, thus mitigating the risk of invalid states. Databases are synonymous with transactions, and many programming languages rely on the underlying database to provide the required transactional support. This works well when all modified state is found at the database level. Unfortunately, this is not always the case and there can be cases where the state is found at the application level rather than the database level. In this article we will see how we can use Multiverse, a Software Transactional Memory, to provide transactions at the software level without using any databases.

Compress Large Text Data in Table

Compress Large Text Data in Table

There can be cases where we need to save large text data into a database tables. This data can take lots of space and may introduce new challenges which needs to be addressed. Backups for example may take longer and if uncompressed, these will take more space. In this article we explore several possibilities available when dealing with large data and compare the results obtained.

How to Lock a File

Locking a file is quite simple in Java. The FileChannel class provides all methods we need to lock files. The tryLock() method will try to obtain the lock on the file without waiting. If the lock is acquired an instance of FileLock is returned, otherwise this method returns null

How to Add Custom File Attributes

The Files class, added in Java 7, provides an easy way to add and retrieve custom file's attributes

Take a Screenshot of Web Page

Selenium is a comprehensive testing framework for web applications which allows us to take screenshots of web pages programmatically amongst many other things. It makes use of web browsers, such as FireFox, to open the web page and then takes the screenshot. This how-to shows how to leverage Selenium to capture screenshots programmatically.

How to Retrieve the Video Properties

How to Retrieve the Video Properties

Videos properties, such as frames per seconds, can be easily obtained using Media Properties library. This third party library provides more than simple frames per second as described in this article.

How to Modify a String Without Creating a New One

How to Modify a String Without Creating a New One

Strings are meant to be immutable, but using reflection we can modify the unmodifiable. This may seem like a shortcoming in the language. Java provides protection against such modifications as described in this article.

Using Flyway with Virgo

Using Flyway with Virgo

Databases are an important part of most applications irrespective of their size and nature. Data needs to be persisted and this is usually done through a database. OSGi is an emerging platform providing high degree of modularity and also supports the hot deployment and replacement of modules. This article describes how to use Flyway, a database migration library, with Eclipse Virgo OSGi implementation (previously known as Spring Dynamic Modules).

The Broken Singleton

The Broken Singleton

The singleton pattern is a very popular pattern, yet it is very hard to implement properly especially when used together with lazy initialisation. This article discusses the common pitfalls may developers fall into when creating their own singletons and shows a way how to design one using the language to our advantage

How to Write InputStream to RandomAccessFile

Writing an InputStream to a RandomAccessFile is quite simple as shown in the following example. This example also makes use of FileLock to make sure that no one modifies the file while the data is being written to file.

What is Race Condition and How to Prevent It?

What is Race Condition and How to Prevent It?

Say that you are eating some sweets while watching a movie. You had a look at your pack or sweets, counted five and continued watching the movie. Took the first one and deducted this in your mind (without looking at the sweets pack). In the mean time, and without you knowing, your daughter takes one. While you think that there are four sweets left, in reality there are only three. You will discover this at the end when you are looking for the last one. This is a Race Condition and in this article we will see how this occurs in programming and how we can avoid it.

What is Defensive Copying?

What is Defensive Copying?

Defensive copying is a technique which mitigates the negative effects caused by unintentional (or intentional) modifications of shared objects. As the title indicates, instead of sharing the original object, we share a copy of it and thus any modification made to the copy will not affect the original object.

How to Force Garbage Collection

The System.gc() does not guarantee that the garbage collection has executed and that the memory was cleaned from unused objects. This post shows you how to force garbage collection.

Testing Swing Application

Testing Swing Application

This tutorial demonstrates how to develop a simple Java Swing (or user interface) application and test the UI related logic using automated tests, such as JUnit, using a series of videos compiled as a playlist. The application is kept very simple while making use of various technologies, such as EasyMock. This tutorial is targeted to college or university students who studied Java but lack experience and would like to do something productive. The main idea of this tutorial is not to learn Java, but to help the viewers to build testable Swing applications. The playlist presented by this tutorial shows how to build a Java Swing application that can be easily tested using automated tests in a step-by-step fashion.

Setting up the Environment

Setting up the Environment

This tutorial provides a step-by-step guide how to setup the development environment on a Windows 7 operating system. Please note that while you can follow this tutorial even if you are using a different operating system, some of the steps shown here may not be the same for all operating systems. For example, a Linux operating system does not install programs under the "Programs Files" folder like a Windows operating system does. On the other hand, the configuration of the SpringSource Tool Suite is the same irrespective of the operating system. Furthermore, irrespective of the operating system, the order of how things are done is the same for all operating systems.

Gson TypeAdapter Example Serialise Large Objects

Gson TypeAdapter Example Serialise Large Objects

Programs work well when dealing with a considerable size of data, but their performance tend to degradate when dealing with large volumes of data. Resources are cheaper than ever, but this does not mean that we can have inefficient programs. Dealing with large amounts of data brings several challenges, even if the task is simple parsing some Java objects into JSON strings, as we will see in this article. Inefficient use of memory can make a program wasting more than 5 times the memory when compared with a more efficient one. This means that one program makes use of 1G of memory, while the inefficient one requires 5G of memory to perform the same task.

Simple Java Database Swing Application

Simple Java Database Swing Application

This tutorial demonstrates how to develop a Java application that connects with a database and provides a user interface and is targeted to students who studied Java, but lack experience. This tutorial assumes basic knowledge of Java and it describes everything in great detail. The main idea of this tutorial is not learn Java, but to help the viewers to gain experience in developing Java applications. During the tutorial we explore various technologies, such as Maven. These technologies are quite popular amongst developers communities and are considered as good traits when applying for jobs.

Gson TypeAdapter Example

Gson TypeAdapter Example

Java objects can be serialised to JSON strings and deserialised back using the JsonSerializer and the JsonDeserializer respectively. These two classes simplify the translation between these two realms but add an extra layer which can be avoided. Instead of the JsonSerialiser or JsonDeserialiser we can use an instance of TypeAdapter which can serialise and deserialise JSON objects efficiently as we will see in this article.

How To Run Embedded Tomcat with Maven

Java web application require a web container, such as Tomcat, on which these can run. Installing and configure a web container on each developing machine may be time consuming. Furthermore, other developers need to manage the dependencies manually if they want to run the web application.

Sorting a CopyOnWriteArrayList

Sorting a CopyOnWriteArrayList

Sorting a List is a very common task and the Java Collections Framework provides methods that perform this, such as the Collections.sort() method. Unfortunately this method will not work on all instances of type List, such as CopyOnWriteArrayList. This article provides a neat and simple way to have of how to obtained an ordered list.

What is Deadlock and How to Prevent It?

What is Deadlock and How to Prevent It?

Imagine you are waiting for a call from your girlfriend with whom you had an argument. She too is waiting for you to call her and apologise. In other words, both of you are waiting for the other to call. Now if no one takes the phone and calls the other, you will never talk again. This is a deadlock and in this article we will see how deadlock occurs in programming and how to prevent it.

How to Cache Results to Boost performance

How to Cache Results to Boost performance

Computing the same results over and over again is a waste of time and resources. Caching can help with that as the result is simply returned instead of recomputing it. While in theory caching is very simple, in practice it can be quite challenging.

Java Fork Join Example

Java Fork Join Example

Java 7 introduced a new type of ExecutorService called Fork/Join Framework, which excels in handling recursive algorithms. Different from other implementations of the ExecutorService, the Fork/Join Framework uses a work-stealing algorithm, which maximise the threads utilisation, and provides a simpler way to deal with tasks which spawn other tasks (referred to as subtasks).

Hello World

Hello World

Hello World is the first program found in most programming books and this blog is not an exception