use of org.nutz.dao.DaoException in project nutz by nutzam.
the class NutDao method fetch.
public <T> T fetch(Class<T> classOfT, String name) {
if (name == null)
throw new IllegalArgumentException("name MUST NOT NULL!");
Entity<T> en = holder.getEntity(classOfT);
if (en.getNameField() == null)
throw new DaoException("Need @Name for " + classOfT);
Pojo pojo = pojoMaker.makeQuery(en).append(Pojos.Items.cndName(en, name)).addParamsBy(name).setAfter(_pojo_fetchEntity);
_exec(pojo);
return pojo.getObject(classOfT);
}
use of org.nutz.dao.DaoException in project nutz by nutzam.
the class NutDao method fetch.
public <T> T fetch(Class<T> classOfT, long id) {
Entity<T> en = holder.getEntity(classOfT);
if (en.getIdField() == null)
throw new DaoException("Need @Id for " + classOfT);
Pojo pojo = pojoMaker.makeQuery(en).append(Pojos.Items.cndId(en, id)).addParamsBy(id).setAfter(_pojo_fetchEntity);
_exec(pojo);
return pojo.getObject(classOfT);
}
use of org.nutz.dao.DaoException in project nutz by nutzam.
the class NutTxDao method begin.
/**
* 开启事务
*
* @param transLevel
* 事务级别
* @see java.sql.Connection
* @return 原对象
* @throws DaoException
* 如果已经开启过事务
*/
public NutTxDao begin(int transLevel) throws DaoException {
if (this.conn != null)
throw new DaoException("NutTxDao has been begined!!");
id = R.UU32();
if (debug)
log.debugf("begin level=%d id=%s", transLevel, id);
try {
this.conn = dataSource.getConnection();
this.conn.setTransactionIsolation(transLevel);
if (this.conn.getAutoCommit() == true) {
this.conn.setAutoCommit(false);
_autoCommit = true;
}
setSavepoint(id);
} catch (SQLException e) {
throw new DaoException(e);
}
return this;
}
use of org.nutz.dao.DaoException in project nutz by nutzam.
the class Record method create.
public static void create(Map<String, Object> re, ResultSet rs, ResultSetMetaData meta) {
String name = null;
int i = 0;
try {
if (meta == null)
meta = rs.getMetaData();
int count = meta.getColumnCount();
for (i = 1; i <= count; i++) {
name = meta.getColumnLabel(i);
switch(meta.getColumnType(i)) {
case Types.TIMESTAMP:
{
re.put(name, rs.getTimestamp(i));
break;
}
case Types.DATE:
{
// ORACLE的DATE类型包含时间,如果用默认的只有日期没有时间 from
// cqyunqin
re.put(name, rs.getTimestamp(i));
break;
}
case Types.CLOB:
{
re.put(name, rs.getString(i));
break;
}
default:
re.put(name, rs.getObject(i));
break;
}
}
} catch (SQLException e) {
if (name != null) {
throw new DaoException(String.format("Column Name=%s, index=%d", name, i), e);
}
throw new DaoException(e);
}
}
use of org.nutz.dao.DaoException in project nutz by nutzam.
the class NutDaoRunner method _runWithoutTransaction.
public void _runWithoutTransaction(DataSource dataSource, ConnCallback callback) {
Connection conn = null;
// 开始一个连接
try {
conn = selectDataSource(null, dataSource, callback).getConnection();
// 开始真正运行
runCallback(conn, callback);
// 完成提交
if (!conn.getAutoCommit())
conn.commit();
}// 异常回滚
catch (Exception e) {
try {
if (// 高并发时,从数据库连接池获取连接就已经抛错误,所以conn可能为null的
conn != null)
conn.rollback();
}// TODO 简单记录一下?
catch (Exception e1) {
}
if (e instanceof DaoException)
throw (DaoException) e;
throw new DaoException(e);
} finally // 保证释放资源
{
if (null != conn) {
// 关闭链接
try {
conn.close();
} catch (SQLException closeE) {
if (log.isWarnEnabled())
log.warn("Fail to close connection!", closeE);
}
}
}
}
Aggregations