About This Blog

This blog contains some information about Java programming strongly connected to making web pages, and JEE applications.

26 January 2008 - 18:24Sequoia tests

"Sequoia is a transparent middleware solution offering clustering, load balancing and failover services for any database."

As in current company we have a lot of troubles with MySQL master-slave architecture, I did a small research to find alternative solution. So first solution which I found is Sequoia - to test it I installed two fresh FreeBSD systems with only mysql servers installed, both were configured same. I let to connect from local network, and start prepare another machine to host Sequoia.

I downloaded sequoia-2.10.9-bin.tar.gz, from download section of http://sequoia.continuent.org/HomePage, and start to setup on host machine. First we need to setup path to Sequoia directory e.g. export SEQUOIA_HOME=/home/ghost/sequoia-2.10.9-bin/. Next I had to copy mysql-connector.jar into drivers directory on Sequoia home.

First small problem I have reached was exception at InetAddress.getLocalHost(), it simple occurs when hostname was not set or set but not placed in /etc/hosts file (e.g. line "127.0.0.1 heaven heaven.my.domain" not found). After that I have controller installed, next step was setting up mysql virtual database, so I added to controller config:

 
<VirtualDatabase configFile="mysql.xml" virtualDatabaseName="myDB" autoEnableBackends="force" />
 

In mysql.xml I have configured two databases separately with only different names and URL's:

 
<DatabaseBackend name="10.0.0.10" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://10.0.0.10:3306/sequoia" connectionTestStatement="select 1">
    <ConnectionManager vLogin="user" rLogin="ghost" rPassword="ghost*1">
        <SimpleConnectionManager />
    </ConnectionManager>
</DatabaseBackend>
<DatabaseBackend name="10.0.0.11" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://10.0.0.11:3306/sequoia" connectionTestStatement="select 1">
    <ConnectionManager vLogin="user" rLogin="ghost" rPassword="ghost*1">
        <SimpleConnectionManager />
    </ConnectionManager>
</DatabaseBackend>
 

After successful launching last thing to do was creating simple client and check what databases would have after this step. Of course everything was great, both databases had same records, controller did great job.

 
package com.jreactor;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
 
public class SequoiaSimpleTest {
 
	private final static String JDBCURL = "jdbc:sequoia://127.0.0.1:25322/myDB?user=user&password=";
 
	static Connection connection;
 
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
 
		Class.forName("org.continuent.sequoia.driver.Driver");
 
		connection= DriverManager.getConnection(JDBCURL);
 
		Statement statement = connection.createStatement();
 
		String sql;
		sql = "drop table TestTableA";
		statement.execute(sql);
 
		sql = "create table TestTableA (id int(11) auto_increment primary key, name varchar(255))";
		statement.execute(sql);
 
		for(int i = 0; i < 10; i++) {
			new SequoiaSimpleTest().new SomeInserts(i).start();
		}
	}
 
	class SomeInserts extends Thread {
		private int threadId;
 
		public SomeInserts(int threadId) {
			this.threadId = threadId;
		}
 
		public void run() {
			try {
				StringBuilder sb;
				PreparedStatement statement2 = connection.prepareStatement("insert into TestTableA values (null, ?)");
				Random random = new Random(1000000);
				for(int i = 0; i < 1000; i++) {
					sb = new StringBuilder();
					sb.append(threadId);
					sb.append(" - ");
					sb.append(random.nextInt());
					statement2.setString(1, sb.toString());
					statement2.execute();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
 

No Comments | Tags: Sequoia, databases

11 January 2008 - 23:36Weekend with GWT

How quick is possible to learn GWT? My first impressions, are very good, even I have not full support on FreeBSD, I can still view compiled pages. Learning curve looks very short if you have any experience with e.g. Swing, I started some time ago maybe an hour and get quit interesting results. So we have different panels, form UI elements, labels, trees, menus etc. All Widgets can have their own listeners, which are really simple in use - for example for Tree you can write:

final Tree categoryTree = new Tree();
categoryTree.addTreeListener(new TreeListener() {
    public void onTreeItemSelected(TreeItem treeItem) {
        editLabel.setText(treeItem.getText());
    }
    public void onTreeItemStateChanged(TreeItem treeItem) {
        //nothing
    }
});

Simple example from today's first coding evening you can see here.

Sample HTML Sample Java Source

No Comments | Tags: GWT

3 January 2008 - 14:13Hibernate in Spring

Probably most people use getHibernateTemplate() (extends HibernateDaoSupport) when invoking Hibernate in Spring - it's very good option for most cases, but for beginnings there are some of hidden features, which are used in different way in pure Hibernate. So two of them I have to use today - first was narrowing results when using Criteria - it's very simple when using HibernateTemplate:

public List<QueueElement> getObjectsFromQueue(int first, int count) {
    return getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(QueueElement.class), first, count);
}
 

Using this method we get nicely narrowed results for a given class. The second example considers object counting for given Criteria:

public int getQueueSize() {
    return (Integer) getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(QueueElement.class)
        .setProjection(Projections.rowCount())).get(0);
}

It's very simple way to get rows counted without using Hibernate HibernateCallback and uniqueResult() method, as we can see in older example:

public int getConsumersSize() {
    return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            return session.createCriteria(Consumer.class)
                .setProjection(Projections.rowCount())
                .uniqueResult();
            }
    });
}

No Comments | Tags: hibernate, spring