/**
*
* @author yw04009 Bill,Wu
*
*/
public class TransacitonUtil {
//lacking of some try exception method ,just for speeding up my developing
private static DataSource ds;
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
static {
try {
Properties prop = new Properties();
InputStream in = TransacitonUtil.class.getClassLoader().getResourceAsStream("testTransaciton.properties");
prop.load(in);
BaseDataSourceFactory factory = new BaseDataSourceFactory ();//from dbcp jar
ds = factory.createDataSource(prop);
} catch (Exception e) {
}
}
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection () {
//get the Connection for Current Thread
Connection conn = threadLocal.get();
//if there is not connectio on current thread, should get some one from static DataSource
if (conn == null) {
conn = ds.getConnection();
threadLocal.set(conn);
}
return conn;
}
public static void startTransaciton() {
Connection conn = threadLocal.get();
if (conn == null) {
conn = ds.getConnection();
threadLocal.set(conn);
}
conn.setAutoCommit(false);
}
public static void endTransaciton() {
Connection conn = threadLocal.get();
if (conn != null) {
conn.commit();
}
}
public static void closeTransaciton() {
try {
Connection conn = threadLocal.get();
if (conn != null) {
conn.close();
}
} catch (Exception e) {
} finally {
//Special attentions, remove connections link from currenct datacource container
threadLocal.remove();
}
}
}
*
* @author yw04009 Bill,Wu
*
*/
public class TransacitonUtil {
//lacking of some try exception method ,just for speeding up my developing
private static DataSource ds;
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
static {
try {
Properties prop = new Properties();
InputStream in = TransacitonUtil.class.getClassLoader().getResourceAsStream("testTransaciton.properties");
prop.load(in);
BaseDataSourceFactory factory = new BaseDataSourceFactory ();//from dbcp jar
ds = factory.createDataSource(prop);
} catch (Exception e) {
}
}
public static DataSource getDataSource(){
return ds;
}
public static Connection getConnection () {
//get the Connection for Current Thread
Connection conn = threadLocal.get();
//if there is not connectio on current thread, should get some one from static DataSource
if (conn == null) {
conn = ds.getConnection();
threadLocal.set(conn);
}
return conn;
}
public static void startTransaciton() {
Connection conn = threadLocal.get();
if (conn == null) {
conn = ds.getConnection();
threadLocal.set(conn);
}
conn.setAutoCommit(false);
}
public static void endTransaciton() {
Connection conn = threadLocal.get();
if (conn != null) {
conn.commit();
}
}
public static void closeTransaciton() {
try {
Connection conn = threadLocal.get();
if (conn != null) {
conn.close();
}
} catch (Exception e) {
} finally {
//Special attentions, remove connections link from currenct datacource container
threadLocal.remove();
}
}
}