Thursday, May 6, 2010

JNDI in JBoss

There are three levels of naming scope in JBoss:

- Name underjava:comp, only available to the application component such as EJB Bean1, Web application web1 etc. java:comp/env refers to as enterprise naming context (ENC)

- Names under java:, visible only within the JBoss server and not to remote clients

- Any other names, global visible, provided that the context or object support serialization.

With JNDI reference, it allows us to bind non-serializable object to JNDI. It actually only bind object reference to it and retrieve the object from an ObjectFactory based on the reference. Spring's BeanFactory is bound to JNDI in this way. The limitation is that the object is only available in the same JVM. Even a backend service is serializable, it doesn't make sense to bind it to JNDI directly, because the implementation class gets loaded to client side and unserialized.

A code snippet from JBoss's NonSerializableFactory


public static synchronized void rebind(Context ctx, String key, Object target)
throws NamingException
{
rebind(key, target);
String className = target.getClass().getName();
String factory = (org.jboss.util.naming.NonSerializableFactory.class).getName();
StringRefAddr addr = new StringRefAddr("nns", key);
Reference memoryRef = new Reference(className, addr, factory, null);
ctx.rebind(key, memoryRef);
}

Tuesday, May 4, 2010

Drop database from DOS batch script


mysql -uroot -pabc123 -e "drop database netsight" >nul 2>1&

The option -e to run execute a command. --help (-?) to display help information.

Redirect to null to keep quiet. Otherwise, it may display an error "ERROR 1010 (HY000) at line 1: Error dropping database (can't rmdir '.\netsight', errno: 41)". Since when dropping a database, it will try to delete folder mysql/data/netsight, it fails if the folder contains some non-db files.

Thursday, April 29, 2010

Insert for multiple records


insert into ce_antennaSocket(platformName, socketID, socketName, radioBandID) values
('AP2600', '0', 'Left Antenna Type', 0),
('AP2600', '1', 'Right Antenna Type', 0),
('AP2650', '0', 'Top Left Antenna Type', 0),
('AP2650', '1', 'Bottom Left Antenna Type', 0),
('AP2650', '2', 'Top Right Antenna Type', 0),
('AP4102', '1', 'Right Antenna Type', 1);

It has much much better performance than split into multiple insert statements. To insert 15k records, it took less than 1 second. Otherwise, it took 8 minutes.

Wednesday, April 28, 2010

Class vs. Classloader getResouceAsStream

In bk.sar -
conf/bkConfigSchema.sql
com.xyz.BkWirelessMgr

For classloader.getResouceAsStream(), all names are absolute, no leading "/". For class.getResourceAsStream(), it has relative and absolute path. But relative means starting from the class's package, i.e., without the leading "/", it will search com.xyz/conf/bkConfigSchema.sql, not found and return null.

Then in BkWirelessMgr class, to load the sql,

getClass().getResourceAsStream("/conf/bkConfigSchema.sql");
getClass().getClassLoader().getResourceAsStream("conf/bkConfigSchema.sql");

Note that "/" is used here instead of File.Separator which doesn't work.