Archive April 2014

define specific location for mysql database

You can define different database location with simple symlink
create new database witrDB

$ mysql -u witr -p
> CREATE DATABASE witrDB;
> quit

list all stored databases

$ sudo ls /var/lib/mysql/

witrDB must be listed
stop mysql service

$ sudo service mysql stop

move witrDB to different location

$ sudo mv /var/lib/mysql/witrDB /drive/myDrive/myDatabases/

create symlink

$ sudo ln -s /drive/myDrive/myDatabases/witrDB /var/lib/mysql/witrDB

restart mysql service

$ sudo service mysql start

If mysql server jobs start fails:
1. ensure that linux user have rights on your new database directory

$ chown youruser.yourgroup /drive/myDrive/myDatabases/witrDB

2. look for apparmor config

$ sudo vi /etc/apparmor.d/usr.sbin.mysqld

add these two lines:
/mnt/perso/mysql.perso/ r,
/mnt/perso/mysql.perso/** rwk,

$ sudo service apparmor restart

3. restart mysql service and try again

$ sudo service mysql restart

want to handle json in Java : play with javax.json

First get libraries
With maven for example add these two artifacts

        
        
            javax.json
            javax.json-api
            1.0
        

        
        
            org.glassfish
            javax.json
            1.0.4
        

Parse json String variable (event processing)


    String json = "{"name":"witr","quotes":{"java":"150","linux":"200"}}";

    JsonParserFactory factory = Json.createParserFactory(null);
    JsonParser parser = factory.createParser(new StringReader(json));

    while (parser.hasNext()) {
        JsonParser.Event event = parser.next();

        switch (event) {
            case KEY_NAME: {
                String key = parser.getString();
                System.out.print(key + "="); break;
            }
            case VALUE_STRING: {
                String value = parser.getString();
                System.out.println(value); break;
            }
        }
    }
    
    // output :
    // name=witr
    // quotes=java=150
    // linux=200

    String json = "{"name":"witr","quotes":{"java":150,"linux":200}}";

    JsonReader jsonReader = Json.createReader(new StringReader(json));
    JsonObject jsonObject = jsonReader.readObject();
    System.out.println("name : "+jsonObject.getString("name"));
    System.out.println("quotes : "+jsonObject.getJsonObject("quotes"));
    System.out.println("java : "+jsonObject.getJsonObject("quotes").getInt("java"));
    System.out.println("linux : "+jsonObject.getJsonObject("quotes").getInt("linux"));

    // output :
    // name : witr
    // quotes : {"java":150,"linux":200}
    // java : 150
    // linux : 200

Hibernate exception : cannot simultaneously fetch multiple bags

Problem

Exception class : org.hibernate.loader.MultipleBagFetchException
Exception : cannot simultaneously fetch multiple bags

Solution

Use @LazyCollection(LazyCollectionOption.FALSE) rather than fetch=FetchType.EAGER
Annotation @LazyCollection(LazyCollectionOption.FALSE) makes that collecion is loaded like with FetchType.EAGER and you can use it on two and more collections.

Example

Initial Code : Works with lazy fetch type

public class WitrEntity{

    private Set witrEntityLabels = new HashSet(0);

    [...]

    @OneToMany(fetch=FetchType.LAZY, mappedBy="witrEntity")
    public Set getWitrEntityLabels() {
        return this.witrEntityLabels;
    }

}

public class WitrEntityLabel{

    private WitrEntity witrEntity;

    [...]

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="WITR_ENTITY_ID", nullable=false, insertable=false, updatable=false)
    public WitrEntity getWitrEntity() {
        return this.witrEntity;
    }

}

Wrong manipulation : wanna set fetch type to eager

public class WitrEntity{

    private Set witrEntityLabels = new HashSet(0);

    [...]

    @OneToMany(fetch=FetchType.EAGER, mappedBy="witrEntity") // THIS IS THE CAUSE OF EXCEPTION
    public Set getWitrEntityLabels() {
        return this.witrEntityLabels;
    }

}

public class WitrEntityLabel{

    private WitrEntity witrEntity;

    [...]

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="WITR_ENTITY_ID", nullable=false, insertable=false, updatable=false)
    public WitrEntity getWitrEntity() {
        return this.witrEntity;
    }

}

Solution : if you want to force loading collection

public class WitrEntity{

    private Set witrEntityLabels = new HashSet(0);

    [...]

    @OneToMany(mappedBy="witrEntity") // delete fetch=FetchType.LAZY
    @LazyCollection(LazyCollectionOption.FALSE) // add use @LazyCollection false
    public Set getWitrEntityLabels() {
        return this.witrEntityLabels;
    }
}

Jboss7 (jboss-cli) : how to redirect specified class log in specified file

Don’t edit standalone.xml and then restart jboss, just use jboss-cli commands. Only three steps are enough

1. Add a file log handler

/subsystem=logging/file-handler=HANDLER_NAME:add(file={“path”=>”FILE_NAME“,”relative-to”=>”jboss.server.log.dir”},level=”LEVEL“)

2. Add a log category

/subsystem=logging/logger=CATEGORY_NAME:add(level=”LEVEL“)

3. Add a log handlers to a log category

/subsystem=logging/logger=CATEGORY_NAME:assign-handler(name=”HANDLER_NAME“)

done…

By example : We have to redirect net.witr.MyClass info logs to witr.log file

[witr@WITR-PC]#cd $JBOSS_HOME
[witr@WITR-PC]#./bin/jboss-cli.sh
You are disconnected at the moment. Type 'connect' to connect to the server or 'help' for the list of supported commands.
[disconnected /] connect
[standalone@localhost:9991 /] /subsystem=logging/file-handler=WITR_HANDLER:add(file={"path"=>"witr.log","relative-to"=>"jboss.server.log.dir"},level="INFO")
[standalone@localhost:9991 /] /subsystem=logging/logger=net.witr.MyClass:add(level="INFO")
[standalone@localhost:9991 /] /subsystem=logging/logger=net.witr.MyClass:assign-handler(name="WITR_HANDLER")