Search in sources :

Example 1 with DaoException

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);
}
Also used : Pojo(org.nutz.dao.sql.Pojo) DaoException(org.nutz.dao.DaoException)

Example 2 with DaoException

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);
}
Also used : Pojo(org.nutz.dao.sql.Pojo) DaoException(org.nutz.dao.DaoException)

Example 3 with DaoException

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;
}
Also used : SQLException(java.sql.SQLException) DaoException(org.nutz.dao.DaoException)

Example 4 with DaoException

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);
    }
}
Also used : SQLException(java.sql.SQLException) DaoException(org.nutz.dao.DaoException)

Example 5 with DaoException

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);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DaoException(org.nutz.dao.DaoException) DaoException(org.nutz.dao.DaoException) SQLException(java.sql.SQLException)

Aggregations

DaoException (org.nutz.dao.DaoException)12 SQLException (java.sql.SQLException)4 Test (org.junit.Test)3 Connection (java.sql.Connection)2 Pojo (org.nutz.dao.sql.Pojo)2 Sql (org.nutz.dao.sql.Sql)2 Pet (org.nutz.dao.test.meta.Pet)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Savepoint (java.sql.Savepoint)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 EntityField (org.nutz.dao.entity.EntityField)1 MappingField (org.nutz.dao.entity.MappingField)1 Column (org.nutz.dao.entity.annotation.Column)1 Id (org.nutz.dao.entity.annotation.Id)1 ManyMany (org.nutz.dao.entity.annotation.ManyMany)1 Name (org.nutz.dao.entity.annotation.Name)1 One (org.nutz.dao.entity.annotation.One)1 ManyLinkField (org.nutz.dao.impl.entity.field.ManyLinkField)1