package juan.orm.datasource.impl;
import java.util.Properties;
import javax.sql.DataSource;
import juan.exception.JRuntimeException;
import juan.orm.datasource.IDataSource;
import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
import org.enhydra.jdbc.standard.StandardXADataSource;
import org.objectweb.jotm.Current;
public class XapoolDataSourceImpl implements IDataSource {
private String name=null;
public DataSource getDataSource() {
Properties prop = new Properties();
try {
prop.load(ClassLoader.getSystemResourceAsStream("xapool.properties"));
} catch (Exception e) {
System.err.println("problem to load properties.");
e.printStackTrace();
}
String login = prop.getProperty("login");
String password = prop.getProperty("password");
String url = prop.getProperty("url");
String driver = prop.getProperty("driver");
String maxActive = prop.getProperty("maxActive");
String maxIdle = prop.getProperty("maxIdle");
StandardXADataSource xads = new StandardXADataSource();
StandardXAPoolDataSource spds = new StandardXAPoolDataSource();
try {
xads.setUrl(url);
xads.setDriverName(driver);
xads.setUser(login);
xads.setPassword(password);
xads.setTransactionManager(Current.getCurrent());
xads.setDebug(true);
// second, we create the pool of connection with the previous object
spds.setMaxSize(Integer.parseInt(maxActive));
spds.setMinSize(Integer.parseInt(maxIdle));
spds.setUser(login);
spds.setPassword(password);
spds.setDataSourceName(getName());
spds.setDataSource(xads);
spds.setTransactionManager(Current.getCurrent());
spds.setDebug(true);
} catch (Exception e) {
e.printStackTrace();
throw new JRuntimeException(e);
}
return spds;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
}
package juan.transaction;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import juan.exception.JRuntimeException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.objectweb.jotm.Jotm;
import org.objectweb.transaction.jta.TMService;
/**
*
* @author liuth
* @version 1.0
* Created on 2003-2-20
*/
public class JOTMTransaction implements ITransaction {
private static final Log logger = LogFactory.getLog(JOTMTransaction.class);
private UserTransaction tx;
private TMService jotm = null;
public void initial(){
try {
if(jotm==null)
jotm = new Jotm(true, false);
if(tx==null)
tx = jotm.getUserTransaction();
} catch (Exception ne) {
logger.error(ne);
throw new JRuntimeException(ne);
}
}
public void initial(String jndi){
try {
if(jotm==null)
jotm = new Jotm(true, false);
if(tx==null)
tx = jotm.getUserTransaction();
} catch (Exception ne) {
logger.error(ne);
throw new JRuntimeException(ne);
}
}
public void begin() {
try {
tx.begin();
} catch (SystemException se) {
logger.error(se);
throw new JRuntimeException(se);
} catch (NotSupportedException nse) {
logger.error(nse);
throw new JRuntimeException(nse);
}
}
public void commit() {
try {
tx.commit();
} catch (SystemException se) {
logger.error(se);
throw new JRuntimeException(se);
} catch (HeuristicRollbackException hre) {
logger.error(hre);
throw new JRuntimeException(hre);
} catch (HeuristicMixedException hme) {
logger.error(hme);
throw new JRuntimeException(hme);
} catch (RollbackException re) {
logger.error(re);
throw new JRuntimeException(re);
}finally{
close();
}
}
public void rollback() {
try {
tx.rollback();
} catch (SystemException se) {
logger.error(se);
throw new JRuntimeException(se);
}finally{
close();
}
}
private void close(){
if(jotm!=null){
jotm.stop();
}
}
}