org.apache.ibatis.executor.ErrorContext Java Examples
The following examples show how to use
org.apache.ibatis.executor.ErrorContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MybatisHelper.java From snakerflow with Apache License 2.0 | 6 votes |
/** * 使用DataSource初始化SqlSessionFactory * @param ds 数据源 */ public static void initialize(DataSource ds) { TransactionFactory transactionFactory = new MybatisTransactionFactory(); Environment environment = new Environment("snaker", transactionFactory, ds); Configuration configuration = new Configuration(environment); configuration.getTypeAliasRegistry().registerAliases(SCAN_PACKAGE, Object.class); if (log.isInfoEnabled()) { Map<String, Class<?>> typeAliases = configuration.getTypeAliasRegistry().getTypeAliases(); for(Entry<String, Class<?>> entry : typeAliases.entrySet()) { log.info("Scanned class:[name=" + entry.getKey() + ",class=" + entry.getValue().getName() + "]"); } } try { for(String resource : resources) { InputStream in = Resources.getResourceAsStream(resource); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, resource, configuration.getSqlFragments()); xmlMapperBuilder.parse(); } } catch (Exception e) { e.printStackTrace(); } finally { ErrorContext.instance().reset(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); }
Example #2
Source File: MapperFactoryBean.java From Mapper with MIT License | 6 votes |
/** * {@inheritDoc} */ @Override protected void checkDaoConfig() { super.checkDaoConfig(); notNull(this.mapperInterface, "Property 'mapperInterface' is required"); Configuration configuration = getSqlSession().getConfiguration(); if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) { try { configuration.addMapper(this.mapperInterface); } catch (Exception e) { logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e); throw new IllegalArgumentException(e); } finally { ErrorContext.instance().reset(); } } //直接针对接口处理通用接口方法对应的 MappedStatement 是安全的,通用方法不会出现 IncompleteElementException 的情况 if (configuration.hasMapper(this.mapperInterface) && mapperHelper != null && mapperHelper.isExtendCommonMapper(this.mapperInterface)) { mapperHelper.processConfiguration(getSqlSession().getConfiguration(), this.mapperInterface); } }
Example #3
Source File: DynamicSqlSessionFactory.java From hsweb-framework with Apache License 2.0 | 6 votes |
@SuppressWarnings("all") private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = getConfiguration().getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); DataSource ds = DataSourceHolder.currentDataSource().getNative(); if (ds == null) { ds = environment.getDataSource(); } tx = transactionFactory.newTransaction(ds, level, autoCommit); final Executor executor = getConfiguration().newExecutor(tx, execType); return new DefaultSqlSession(getConfiguration(), executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #4
Source File: SqlSessionFactoryBean.java From Shop-for-JavaWeb with MIT License | 6 votes |
/** * TODO 刷新 * * @param inputStream * @param resource * @param configuration * @throws NestedIOException */ public static void refresh(java.io.InputStream inputStream, String resource, Configuration configuration) throws NestedIOException { try { XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder( inputStream, configuration, resource, configuration.getSqlFragments()); xmlMapperBuilder.parse1(); } catch (Exception e) { throw new NestedIOException("Failed to parse mapping resource: '" + resource + "'", e); } finally { ErrorContext.instance().reset(); } }
Example #5
Source File: DefaultSqlSessionFactory.java From mybatis with Apache License 2.0 | 6 votes |
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); //通过事务工厂来产生一个事务 tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //生成一个执行器(事务包含在执行器里) final Executor executor = configuration.newExecutor(tx, execType); //然后产生一个DefaultSqlSession return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { //如果打开事务出错,则关闭它 closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { //最后清空错误上下文 ErrorContext.instance().reset(); } }
Example #6
Source File: DefaultSqlSessionFactory.java From mybaties with Apache License 2.0 | 6 votes |
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); //通过事务工厂来产生一个事务 tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); //生成一个执行器(事务包含在执行器里) final Executor executor = configuration.newExecutor(tx, execType); //然后产生一个DefaultSqlSession return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { //如果打开事务出错,则关闭它 closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { //最后清空错误上下文 ErrorContext.instance().reset(); } }
Example #7
Source File: ZebraMapperFactoryBean.java From Zebra with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} */ @Override protected void checkDaoConfig() { super.checkDaoConfig(); notNull(this.mapperInterface, "Property 'mapperInterface' is required"); Configuration configuration = getSqlSession().getConfiguration(); if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) { try { configuration.addMapper(this.mapperInterface); } catch (Throwable t) { logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", t); throw new IllegalArgumentException(t); } finally { ErrorContext.instance().reset(); } } }
Example #8
Source File: DefaultSqlSession.java From mybatis with Apache License 2.0 | 5 votes |
@Override public int update(String statement, Object parameter) { try { //每次要更新之前,dirty标志设为true dirty = true; MappedStatement ms = configuration.getMappedStatement(statement); //转而用执行器来update结果 return executor.update(ms, wrapCollection(parameter)); } catch (Exception e) { throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #9
Source File: XMLConfigBuilder.java From mybaties with Apache License 2.0 | 5 votes |
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { //首先调用父类初始化Configuration super(new Configuration()); //错误上下文设置成SQL Mapper Configuration(XML文件配置),以便后面出错了报错用吧 ErrorContext.instance().resource("SQL Mapper Configuration"); //将Properties全部设置到Configuration里面去 this.configuration.setVariables(props); this.parsed = false; this.environment = environment; this.parser = parser; }
Example #10
Source File: DefaultParameterHandler.java From mybatis with Apache License 2.0 | 5 votes |
@Override public void setParameters(PreparedStatement ps) throws SQLException { ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); if (parameterMappings != null) { //循环设参数 for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { //如果不是OUT,才设进去 Object value; String propertyName = parameterMapping.getProperty(); if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params //若有额外的参数, 设为额外的参数 value = boundSql.getAdditionalParameter(propertyName); } else if (parameterObject == null) { //若参数为null,直接设null value = null; } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { //若参数有相应的TypeHandler,直接设object value = parameterObject; } else { //除此以外,MetaObject.getValue反射取得值设进去 MetaObject metaObject = configuration.newMetaObject(parameterObject); value = metaObject.getValue(propertyName); } TypeHandler typeHandler = parameterMapping.getTypeHandler(); JdbcType jdbcType = parameterMapping.getJdbcType(); if (value == null && jdbcType == null) { //不同类型的set方法不同,所以委派给子类的setParameter方法 jdbcType = configuration.getJdbcTypeForNull(); } typeHandler.setParameter(ps, i + 1, value, jdbcType); } } } }
Example #11
Source File: DefaultSqlSession.java From mybatis with Apache License 2.0 | 5 votes |
@Override public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) { try { //根据statement id找到对应的MappedStatement MappedStatement ms = configuration.getMappedStatement(statement); //转而用执行器来查询结果,注意这里传入的ResultHandler是null return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER); } catch (Exception e) { throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #12
Source File: DefaultSqlSession.java From mybatis with Apache License 2.0 | 5 votes |
@Override public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) { try { MappedStatement ms = configuration.getMappedStatement(statement); executor.query(ms, wrapCollection(parameter), rowBounds, handler); } catch (Exception e) { throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #13
Source File: HierarchicalXMLConfigBuilder.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
public HierarchicalXMLConfigBuilder(HierarchicalResourceLoader resourceLoader, InputStream inputStream, String environment, Properties props) { super(new Configuration()); // EXTENDED this.resourceLoader = resourceLoader; ErrorContext.instance().resource("SQL Mapper Configuration"); this.configuration.setVariables(props); this.parsed = false; this.environment = environment; this.parser = new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()); }
Example #14
Source File: DefaultSqlSession.java From mybatis with Apache License 2.0 | 5 votes |
@Override public void commit(boolean force) { try { //转而用执行器来commit executor.commit(isCommitOrRollbackRequired(force)); //每次commit之后,dirty标志设为false dirty = false; } catch (Exception e) { throw ExceptionFactory.wrapException("Error committing transaction. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #15
Source File: DefaultSqlSession.java From mybatis with Apache License 2.0 | 5 votes |
@Override public void rollback(boolean force) { try { //转而用执行器来rollback executor.rollback(isCommitOrRollbackRequired(force)); //每次rollback之后,dirty标志设为false dirty = false; } catch (Exception e) { throw ExceptionFactory.wrapException("Error rolling back transaction. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #16
Source File: DefaultSqlSession.java From mybatis with Apache License 2.0 | 5 votes |
@Override public List<BatchResult> flushStatements() { try { //转而用执行器来flushStatements return executor.flushStatements(); } catch (Exception e) { throw ExceptionFactory.wrapException("Error flushing statements. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #17
Source File: DefaultSqlSession.java From mybatis with Apache License 2.0 | 5 votes |
@Override public void close() { try { //转而用执行器来close executor.close(isCommitOrRollbackRequired(false)); //每次close之后,dirty标志设为false dirty = false; } finally { ErrorContext.instance().reset(); } }
Example #18
Source File: XMLConfigBuilder.java From mybatis with Apache License 2.0 | 5 votes |
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { //首先调用父类初始化Configuration super(new Configuration()); //错误上下文设置成SQL Mapper Configuration(XML文件配置),以便后面出错了报错用吧 ErrorContext.instance().resource("SQL Mapper Configuration"); //将Properties全部设置到Configuration里面去 this.configuration.setVariables(props); this.parsed = false; this.environment = environment; this.parser = parser; }
Example #19
Source File: XMLConfigBuilder.java From QuickProject with Apache License 2.0 | 5 votes |
private XMLConfigBuilder(XPathParser parser, String environment, Properties props) { super(new Configuration()); ErrorContext.instance().resource("SQL Mapper Configuration"); this.configuration.setVariables(props); this.parsed = false; this.environment = environment; this.parser = parser; }
Example #20
Source File: DefaultParameterHandler.java From mybaties with Apache License 2.0 | 5 votes |
@Override public void setParameters(PreparedStatement ps) throws SQLException { ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); if (parameterMappings != null) { //循环设参数 for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { //如果不是OUT,才设进去 Object value; String propertyName = parameterMapping.getProperty(); if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params //若有额外的参数, 设为额外的参数 value = boundSql.getAdditionalParameter(propertyName); } else if (parameterObject == null) { //若参数为null,直接设null value = null; } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { //若参数有相应的TypeHandler,直接设object value = parameterObject; } else { //除此以外,MetaObject.getValue反射取得值设进去 MetaObject metaObject = configuration.newMetaObject(parameterObject); value = metaObject.getValue(propertyName); } TypeHandler typeHandler = parameterMapping.getTypeHandler(); JdbcType jdbcType = parameterMapping.getJdbcType(); if (value == null && jdbcType == null) { //不同类型的set方法不同,所以委派给子类的setParameter方法 jdbcType = configuration.getJdbcTypeForNull(); } typeHandler.setParameter(ps, i + 1, value, jdbcType); } } } }
Example #21
Source File: MybatisXmlProcess.java From springboot-plugin-framework-parent with Apache License 2.0 | 5 votes |
/** * 加载xml资源 * @param resources resources * @param pluginClassLoader pluginClassLoader * @throws Exception Exception */ public void loadXmlResource(List<Resource> resources, ClassLoader pluginClassLoader) throws Exception { if(resources == null || resources.isEmpty()){ return; } Configuration configuration = factory.getConfiguration(); // removeConfig(configuration); ClassLoader defaultClassLoader = Resources.getDefaultClassLoader(); try { Resources.setDefaultClassLoader(pluginClassLoader); for (Resource resource :resources) { InputStream inputStream = resource.getInputStream(); try { PluginMybatisXmlMapperBuilder xmlMapperBuilder = new PluginMybatisXmlMapperBuilder( inputStream, configuration, resource.toString(), configuration.getSqlFragments(), pluginClassLoader); xmlMapperBuilder.parse(); } finally { if(inputStream != null){ inputStream.close(); } } } } finally { ErrorContext.instance().reset(); Resources.setDefaultClassLoader(defaultClassLoader); } }
Example #22
Source File: DefaultParameterHandler.java From AsuraFramework with Apache License 2.0 | 5 votes |
public void setParameters(PreparedStatement ps) throws SQLException { ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); if (parameterMappings != null) { MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject); for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { Object value; String propertyName = parameterMapping.getProperty(); if (boundSql.hasAdditionalParameter(propertyName)) { // issue #448 ask first for additional params value = boundSql.getAdditionalParameter(propertyName); } else if (parameterObject == null) { value = null; } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { value = parameterObject; } else { value = metaObject == null ? null : metaObject.getValue(propertyName); } TypeHandler typeHandler = parameterMapping.getTypeHandler(); if (typeHandler == null) { throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId()); } JdbcType jdbcType = parameterMapping.getJdbcType(); if (value == null && jdbcType == null) jdbcType = configuration.getJdbcTypeForNull(); typeHandler.setParameter(ps, i + 1, value, jdbcType); } } } }
Example #23
Source File: SQLHelper.java From Shop-for-JavaWeb with MIT License | 5 votes |
/** * 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler * * @param ps 表示预编译的 SQL 语句的对象。 * @param mappedStatement MappedStatement * @param boundSql SQL * @param parameterObject 参数对象 * @throws java.sql.SQLException 数据库异常 */ @SuppressWarnings("unchecked") public static void setParameters(PreparedStatement ps, MappedStatement mappedStatement, BoundSql boundSql, Object parameterObject) throws SQLException { ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); if (parameterMappings != null) { Configuration configuration = mappedStatement.getConfiguration(); TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); MetaObject metaObject = parameterObject == null ? null : configuration.newMetaObject(parameterObject); for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { Object value; String propertyName = parameterMapping.getProperty(); PropertyTokenizer prop = new PropertyTokenizer(propertyName); if (parameterObject == null) { value = null; } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { value = parameterObject; } else if (boundSql.hasAdditionalParameter(propertyName)) { value = boundSql.getAdditionalParameter(propertyName); } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX) && boundSql.hasAdditionalParameter(prop.getName())) { value = boundSql.getAdditionalParameter(prop.getName()); if (value != null) { value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length())); } } else { value = metaObject == null ? null : metaObject.getValue(propertyName); } @SuppressWarnings("rawtypes") TypeHandler typeHandler = parameterMapping.getTypeHandler(); if (typeHandler == null) { throw new ExecutorException("There was no TypeHandler found for parameter " + propertyName + " of statement " + mappedStatement.getId()); } typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType()); } } } }
Example #24
Source File: MapperLoader.java From Shop-for-JavaWeb with MIT License | 5 votes |
public void reloadXML() throws Exception { SqlSessionFactory factory = context.getBean(SqlSessionFactory.class); Configuration configuration = factory.getConfiguration(); // 移除加载项 removeConfig(configuration); // 重新扫描加载 for (String basePackage : basePackages) { Resource[] resources = getResource(basePackage, XML_RESOURCE_PATTERN); if (resources != null) { for (int i = 0; i < resources.length; i++) { if (resources[i] == null) { continue; } try { XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(resources[i].getInputStream(), configuration, resources[i].toString(), configuration.getSqlFragments()); xmlMapperBuilder.parse(); } catch (Exception e) { throw new NestedIOException("Failed to parse mapping resource: '" + resources[i] + "'", e); } finally { ErrorContext.instance().reset(); } } } } }
Example #25
Source File: DefaultSqlSession.java From mybaties with Apache License 2.0 | 5 votes |
@Override public void close() { try { //转而用执行器来close executor.close(isCommitOrRollbackRequired(false)); //每次close之后,dirty标志设为false dirty = false; } finally { ErrorContext.instance().reset(); } }
Example #26
Source File: DefaultSqlSession.java From mybaties with Apache License 2.0 | 5 votes |
@Override public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) { try { //根据statement id找到对应的MappedStatement MappedStatement ms = configuration.getMappedStatement(statement); //转而用执行器来查询结果,注意这里传入的ResultHandler是null return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER); } catch (Exception e) { throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #27
Source File: DefaultSqlSession.java From mybaties with Apache License 2.0 | 5 votes |
@Override public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) { try { MappedStatement ms = configuration.getMappedStatement(statement); executor.query(ms, wrapCollection(parameter), rowBounds, handler); } catch (Exception e) { throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #28
Source File: DefaultSqlSession.java From mybaties with Apache License 2.0 | 5 votes |
@Override public int update(String statement, Object parameter) { try { //每次要更新之前,dirty标志设为true dirty = true; MappedStatement ms = configuration.getMappedStatement(statement); //转而用执行器来update结果 return executor.update(ms, wrapCollection(parameter)); } catch (Exception e) { throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #29
Source File: DefaultSqlSession.java From mybaties with Apache License 2.0 | 5 votes |
@Override public void commit(boolean force) { try { //转而用执行器来commit executor.commit(isCommitOrRollbackRequired(force)); //每次commit之后,dirty标志设为false dirty = false; } catch (Exception e) { throw ExceptionFactory.wrapException("Error committing transaction. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
Example #30
Source File: DefaultSqlSession.java From mybaties with Apache License 2.0 | 5 votes |
@Override public void rollback(boolean force) { try { //转而用执行器来rollback executor.rollback(isCommitOrRollbackRequired(force)); //每次rollback之后,dirty标志设为false dirty = false; } catch (Exception e) { throw ExceptionFactory.wrapException("Error rolling back transaction. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }