📄 basebean.java
字号:
/**
*
*/
package com.ufmobile.common;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
import com.ufmobile.common.message.CommonMessage;
import com.ufmobile.platform.Exception.BusinessException;
import com.ufmobile.platform.log.RunTimeLogger;
/**
* <p>
* SessinBean的基类。
* <p>
* 创建日期:2006-12-8
*
* @author qbh
* @since v3.0
*/
public abstract class BaseBean {
@PersistenceContext(unitName = ConstBean.PersistenceContextUnitName)
private EntityManager manager;
@Resource
SessionContext ctx;
private static final String EAR_PAK_NAME = "mstreet";
public EntityManager getManager() {
return manager;
}
public void setManager(EntityManager manager) {
this.manager = manager;
}
public EntityManager getManager(Class c) {
return manager;
}
/**
* <p>
* 取得DataSource
* <p>
* 作者:lgb <br>
* 日期:Dec 20, 2006
*
* @return
*/
// public DataSource getDataSource() {
// DataSource ds = (DataSource) ctx.lookup("java:/MStreetOracleDS");
// return ds;
// }
public DataSource getDataSource(Long SysID) {
DataSource ds = (DataSource) ctx.lookup("java:/MStreetOracleDS" + SysID);
return ds;
}
/**
* <p>
* 取得connection
* <p>
* 作者:msf <br>
* 日期:Dec 20, 2006
*
* @return
*/
// public Connection getConnection() {
// try {
// DataSource ds = getDataSource();
// return ds.getConnection();
// } catch (Exception e) {
// RunTimeLogger.error(this, e.getMessage(), e);
// }
// return null;
// }
public Connection getConnection(Long sysID) throws BusinessException {
Connection conn = null;
try {
DataSource ds = getDataSource(sysID);
conn = ds.getConnection();
if (conn == null) {
throw new BusinessException(this, "无法获取sysID为“" + sysID + "”的Connection!");
}
} catch (SQLException e) {
RunTimeLogger.error(this, e.getMessage(), e);
throw new BusinessException(this, CommonMessage.NETWORK_BUSY);
}
return conn;
}
protected SessionContext getContext() {
return ctx;
}
/**
* <p>
* ejb内部调用本地bean
* <p>
* 作者:msf <br>
* 日期:Dec 20, 2006
*
* @param sBeanName
* @return
*/
protected Object getBean(int StreetId, String sBeanName) {
return ctx.lookup(EAR_PAK_NAME + StreetId + "/" + sBeanName + "/local");
}
public EntityManager getManager(Long sysid) {
return EntityFactory.getInstance().getEntityManager(sysid);
}
protected void close(Connection con, Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
RunTimeLogger.error(this, "无法关闭连接", e);
}
}
protected void close(Connection con, PreparedStatement stmt) {
try {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
RunTimeLogger.error(this, "无法关闭连接", e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -