Wednesday 21 March 2012

Spring support multiple @Transactional datasource

1. Define tx-manager

    <tx:annotation-driven/>
    
        <property name="dataSource" ref="dataSource1"/>
    

    
        <property name="dataSource" ref="dataSource2"/>
    



2. In you service layer code
@Transactional(value = "jdbcTxManager")
public List<String> getNames() {
    return userDao.getNames();
}

@Transactional(value = "jpaTxManager")
public void persitentUser(User user) {
    return user.persist();
}

Friday 2 March 2012

get directory of current class in java

URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
String path = URLDecoder.decode(url.getPath(), "utf-8"); //important: if directory has space or chinese words
File file= new File(path);
System.out.println("directory is: " + file.getPath());

Convert file path to URL and vice verse in java

1 URL --> File path
URL url = ...;
File f = new File(url.getPath());

2. File Path to url
File f =...;

URI uri = file.toURI();
URL url = uri.toURL();