Saturday, September 12, 2009

Slow typing with Dell Inspiron 1501 laptop

Problem: Slow typing in any application, selecting more items using CTRL almost impossible, shut down was slow and sometimes blocked. This problem appeared on my laptop, and I first thought it was a virus, but not virus was found. I reinstalled Windows XP SP2 and drivers but the problem persisted.

Solution: Check your running processes in Windows Task Manager. Do you see ati2evxx running twice? That is the ATI HotKey Pooler Service. Go to Control Panel > Administrative Tools > Services, find this service and disable it. You should find it on "Automatic". Restart computer.

Variation of the problem: The slow typing problem appeared also on Dell Inspiron 1520. There the cause was even stranger: adding contacts to Outlook Express 6. You can read about it here: Andrew Gregory - Slow typing on my laptop

Monday, May 25, 2009

Model View Controller (MVC) Pattern

The Model View Controller (MVC) pattern implements the Separation of Concerns Principle at application level. Back-end and front-end logic are separated with a controller layer that glues them together while keeping them separated at the same time. This is why complex application are more manageable and maintainable when implementing this pattern. The separation between the two makes it easy to change the implementation of one, without changing the implementation of the other. The controller is the one that will suffer changes in order to the new implementation.


The model represents the data and the business rules of your application. It usually referred to as the back-end and consists of two layers: the persistence layer, and the business layer. The persistence layer is responsible for retrieving and storing data in a database while the business layer implements the business rules that manage the data. The model may notify the view about changes in its state.

The view is the layer that implements the interface with the user. The interface displays the data of the model in a human understandable manner. The view may query the state of the model or respond to model state change notifications to keep itself synchronized with the model.

The controller translates user actions into actions that activate business processes in the model. Running this business processes usually produce changes in the state of the model. The controller chooses which view to display to the user as a result of a user action. When the chosen view will be rendered, the current state of the model will be displayed to the user.

Wednesday, May 6, 2009

JPA does not delete orphans

Don't really understand way, but there seems to be a limitation in the framework that does not permit this. If you search the net you'll find many frustrated programmers because of this.

There are some solutions. These solutions imply that the foreign key in the child table allows null. Without this, we can not obtain orphans (a row in the child table which has the foreign key to the parent set on null), and you will get an exception before having the "not deleted orphans" problem. If you need that constraint: foreign key in child must not allow null, there may be another solution which I hope to make time and test so I can post it here soon.

1. If you use a Hibernate implementation behind JPA, you can use the Hibernate annotation @Cascade(CascadeType.DELETE_ORPHAN) .

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@OneToMany
@Cascade(CascadeType.DELETE_ORPHAN)
private List childList;

<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-annotations</artifactid>
<version>3.4.0.GA</version>
</dependency>

2. Manually delete the orpnas after each update that denerate orphans.

entityManager.merge(parentEntity);
em.flush();

//delete orphans
em.createQuery(
"DELETE ChildEntity child WHERE child.parentEntity IS NULL").executeUpdate();

Well, that's life, you got to be a killer from time to time... poor orphans

If you have questions, just ask.

Monday, April 27, 2009

h:messages for a4j requests

Well, it's simple: validation errors are not displayed on the page when they apear while processing a Richfaces AJAX request.

To correct this you have to decorate the h:messages tag like this:

<a4j:outputPanel ajaxRendered="true">
<h:messages/>
</a4j:outputPanel>

Why doesn't it work otherwise?
  • h:messages component is not inserted in the JSF component tree the first time the page is loaded (with an empty form);
  • Richfaces AJAX calls can not work with components which are not already in the JSF component tree.

Why does the decorated version work?
  • a4j:outputPanel inserts in the component tree even non-present child components.
  • ajaxRendered attribute sets the outputPanel (with all its child components) to be rendered with the new values after each AJAX request processing.

Saturday, March 14, 2009

Using JDBC with MySQL

To write Java programs that use a MySQL database you need to do four things:
  1. Install MySQL Server (there will come an article on this in the future)
  2. Install JDK and Eclipse
  3. Install the MySQL Connector/J
  4. Write a test program
In this article details steps 3 and 4.


Install the MySQL Connector/J

The package java.sql in the JDK provides interfaces for working with the database. The implementation of this interfaces is particular to each Database Mangement System (DBMS) (i.e. Oracle, MsSql, MySQL, etc.) and is provided through drivers. In this way your code is independent of DBMS. When you decide to change the DBMS you just need to change the driver, while your code stays the same.

MySQL Connector/J is a native Java driver that converts JDBC (Java Database Connectivity) calls into the network protocol used by the MySQL database.

Installing MySQL Connector/J:

  • download MySQL Connector/J 5.0 (or other version)
  • unpack the archive and copy mysql-connector-java-5.0.8-bin.jar in jre1.6.0_02\lib\ext directory. (jre1.6.0_02 can have different sufix depending on the JDK version you have installed; you can find it in the directory where you installed the JDK.)
This is all what it takes to install the driver for MySQL. Information was taken from the driver archive: \docs\README.txt (1.2.2. Installing the Driver and Configuring the CLASSPATH).


Write a test program

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
* Test connection to a MySQL database installed on local machine.
*
* @author Mihai Ionut Rus
*/
public class TestMySQL {

public static void main(String[] args) {
try {
Statement stmt;

// Register the JDBC driver for MySQL.
Class.forName("com.mysql.jdbc.Driver");

String url = "jdbc:mysql://localhost:3306/mysql";

/*
* Get a connection to the database for a user named root with a blank
* password. If have a password for root, you must provide it.
*/
Connection con = DriverManager.getConnection(url, "root", "");

System.out.println("URL: " + url);
System.out.println("Connection: " + con);

//Create a test database
stmt = con.createStatement();
stmt.executeUpdate("CREATE DATABASE JunkDB");

con.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}