May
18
2010

How to Configure Spring beans.xml File: Basic Usage

how to configure spring beans.xml file with more flexibility

Spring is a very flexible java framework. In spring, beans are the main building bricks of an application while the bean configuration file (mostly beans.xml) acts as cement that glues the beans together.

The file beans.xml also allows beans to be initialized with particular values. For example, you can set the JDBC driver, user name, password for a java bean that is in charge of database connection in the beans.xml file as the following example.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean id="dbManager" class="com.mytechtip.example.DbManager">
		<property name="jdbcDriver" value="com.mysql.jdbc.Driver"/>
		<property name="jdbcUrl" value="jdbc:mysql://localhost/app"/>
		<property name="jdbcUser" value="app" />
		<property name="jdbcPassword" value="app" />
	</bean>
</beans>

So in the java class “com.example.app.DbManager”, you can make JDBC connections with the fields/properties value set as in the beans.xml file. The DbManager class may look as simple as the following:

package com.mytechtip.example;
 
import java.sql.Connection;
import java.sql.DriverManager;
 
public class DbManager {
 
	private String jdbcDriver;
	private String jdbcUrl;
	private String jdbcUser;
	private String jdbcPassword;
 
	public String getJdbcDriver() {
		return jdbcDriver;
	}
	public void setJdbcDriver(String jdbcDriver) {
		this.jdbcDriver = jdbcDriver;
	}
 
        // more getters and setters ...
 
	public Connection getConnection() throws Exception {
             Class.forName (getJdbcDriver()).newInstance();
             return DriverManager.getConnection(
                   getJdbcUrl(), getJdbcUser(), getJdbcPassword());
	}
}

There is one note: These properties are set after the constructor of the bean is called. So if you try to make a JDBC connection in the constructor with the fields, you will mostly get exceptions.

Anyway, this is a very simple example of the bean configuration. We will show more examples to learn the flexibility of the bean configuration in Spring.

Mar
24
2010

Remember to Clear Entity Manager if Entities No Longer Used

Recently  a lesson was learned about the proper use of Java JPA to persist a large number of records into database in a batch process.

Some times it is necessary to load the records line by line from a large file and insert into the database table. There can be several ways to do this:

  • Use the database provided tool to load the data into table. It can be a special sql command or a special stored procedure. This method is quite database specific and not portable.
  • Write the program to load the data into the database table (Using JDBC if the program is written in Java).  This method can be applied to different databases if the insert SQL statement conforms to a level of standard.
  • Use JPA to persist the object one by one. This allows for changing database more easily (under the condition that the JPA provider supports the database.

We used the third options to load the records into the database as we want to support multiple databases without  low level tweaking. However, the first implementation seemed very slow compared with the second option. And it became slower and slower when process of persisting records  progressed.

After a few checking, we realized that we didn’t call the method “clear” of the class EntityManager each time we persisted the object.  The EntityManager kept managing these objects so the resource was not released immediately, making it become slower and slower.

Once we add the “clear” method call, the performance became acceptable. So lesson learned about the use of JPA.

Nov
5
2009

Bad Request Error with apache mod_rewrite

If you use apache module mod_alias together with module mod_rewrite, you probably encounter “Bad Request” error if the settings are not correctly configured.


For example, if you want to make the folder “/opt/mysite_v1.2″ have the alias as “/mysite” so it can be accessed as http://www.mydomain.com/mysite. And you want to have clean url “/mysite/page1″ mapped to “/mysite/index.php?p=page1″.

To define the alias, just add the following in the httpd.conf file:

Alias /mysite/ “/opt/mysite_v1.2/”
<Directory “/opt/mysite_v1.2/”>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>

To allow clean URL. you need to have the following in the “.htaccess” file in the folder “/opt/mysite_v1.2″

RewriteEngine On
# Rewrite base
RewriteBase   /mysite

#  now the rewriting rules
RewriteRule   ^([0-9a-zA-Z_]+)$  index.php?p=$1

Please NOTE that “RewriteBase” Directive is very essential. Without it, you will get the well known “Bad Request” Error.

More information can be found in Apache mod_rewrite documentation.

Oct
14
2009

Use OpenJPA with MySQL: Some Tips


OpenJPA is an implementation of the JPA (Java Persistence API) standards. Initially, I used OpenJPA with Derby which is also known as JavaDB. It works well. However, when i tried to use MySQL as the backend database, I encountered a few issues. Here are a few small tips i would like to share when using OpenJPA with MySQL:

Change the default storage engine

By default, OpenJPA uses innodb as the storage engine. While my application does not need to support transactions and other fancy db features, i’d like to use more plain and “traditional” myisam storage engine. To do so, I just need to set the property “openjpa.jdbc.DBDictionary” with the value “TableType=myisam”. This property object is the one passed to the method “Persistence.createEntityManagerFactory(String, Properties)”.

Allow to store bigger objects (blobs)
By default, OpenJPA maps the java object to mysql data type “blob”. However, as you may be aware of, blob type in mysql only can store up to 65536(216) bytes of data (check http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html), which is quite smaller than the blob in JavaDB or Derby (2G).

Mysql does provide other data types which supports larger storage. mediumblob supports up to 16M while longblob up to 4G. So if I want to store objects other than 64K, I need to change the mapping in OpenJPA. To change the mapping, just set the same property “openjpa.jdbc.DBDictionary”, but with value “blobTypeName=mediumblob”.

If I want both myisam as storage engine and mediumblob as mapped blob type, the property should be set as: “TableType=myisam,blobTypeName=mediumblob”.

Avoid the error “Packet too large”
Now i can save objects up to 16M in size to the database. I run my program, only to find another exception popped up. It says something like “Packet too large”.

This requires the change of mysql server setting as indicated in mysql documentation. The setting “max_allowed_packet“, default to 1M bytes, is too small in my case.

Sep
10
2009

Methods/Software to convert RMVB to AVI

Most current DVD players support playback of Divx or Xvid (usually files with .avi extention) encoded files, but few supports RMVB files which are in real media format.

If you have some RMVB files and want to play them on the DVD player, Then you need to convert the RMVB format to AVI.

Currently, I found out two methods to do this.

The first method is from this article: “How to Convert RMVB“. This method requires you install/download several pieces of software to do this. It’s a bit troublesome, but the article provides very detailed step-by-step instructions and it works.

The other method is to use the open source software alltoavi. It’s quite straight forward to use this software to convert RMVB. And it seems that this method takes less time to do the conversion compared with the first method.

The above two methods both use free software to accomplish the task. There are of course other software that can do this task and they may allow you to have better control for the conversion. Well, you have to pay for it.