org.apache.ibatis.transaction.TransactionFactory Java Examples
The following examples show how to use
org.apache.ibatis.transaction.TransactionFactory.
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: BindingTest.java From mybaties with Apache License 2.0 | 6 votes |
@BeforeClass public static void setup() throws Exception { DataSource dataSource = BaseDataTest.createBlogDataSource(); BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL); BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("Production", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.setLazyLoadingEnabled(true); configuration.getTypeAliasRegistry().registerAlias(Blog.class); configuration.getTypeAliasRegistry().registerAlias(Post.class); configuration.getTypeAliasRegistry().registerAlias(Author.class); configuration.addMapper(BoundBlogMapper.class); configuration.addMapper(BoundAuthorMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); }
Example #2
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 #3
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 #4
Source File: XMLConfigBuilder.java From mybatis with Apache License 2.0 | 6 votes |
private void environmentsElement(XNode context) throws Exception { if (context != null) { if (environment == null) { environment = context.getStringAttribute("default"); } for (XNode child : context.getChildren()) { String id = child.getStringAttribute("id"); //循环比较id是否就是指定的environment if (isSpecifiedEnvironment(id)) { //7.1事务管理器 TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager")); //7.2数据源 DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource")); DataSource dataSource = dsFactory.getDataSource(); Environment.Builder environmentBuilder = new Environment.Builder(id) .transactionFactory(txFactory) .dataSource(dataSource); configuration.setEnvironment(environmentBuilder.build()); } } } }
Example #5
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 #6
Source File: HierarchicalXMLConfigBuilder.java From alfresco-repository with GNU Lesser General Public License v3.0 | 6 votes |
private void environmentsElement(XNode context) throws Exception { if (context != null) { if (environment == null) { environment = context.getStringAttribute("default"); } for (XNode child : context.getChildren()) { String id = child.getStringAttribute("id"); if (isSpecifiedEnvironment(id)) { TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager")); DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource")); DataSource dataSource = dsFactory.getDataSource(); Environment.Builder environmentBuilder = new Environment.Builder(id) .transactionFactory(txFactory) .dataSource(dataSource); configuration.setEnvironment(environmentBuilder.build()); } } } }
Example #7
Source File: BaseDb.java From live-chat-engine with Apache License 2.0 | 6 votes |
public BaseDb(DataSource ds, Props props, String url) { this.ds = ds; this.props = props; this.dialect = props.getStrVal(PropKey.db_dialect); String mappersPackageName = getClass().getPackage().getName(); //mybatis TransactionFactory txFactory = new JdbcTransactionFactory(); Environment environment = new Environment("prod", txFactory, ds); Configuration config = new Configuration(environment); config.addMappers(mappersPackageName, BaseMapper.class); mappers = config.getMapperRegistry().getMappers(); sessionFactory = new SqlSessionFactoryBuilder().build(config); universal = new UniversalQueries(ds, props, url); }
Example #8
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 #9
Source File: XMLConfigBuilder.java From mybaties with Apache License 2.0 | 6 votes |
private void environmentsElement(XNode context) throws Exception { if (context != null) { if (environment == null) { environment = context.getStringAttribute("default"); } for (XNode child : context.getChildren()) { String id = child.getStringAttribute("id"); //循环比较id是否就是指定的environment if (isSpecifiedEnvironment(id)) { //7.1事务管理器 TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager")); //7.2数据源 DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource")); DataSource dataSource = dsFactory.getDataSource(); Environment.Builder environmentBuilder = new Environment.Builder(id) .transactionFactory(txFactory) .dataSource(dataSource); configuration.setEnvironment(environmentBuilder.build()); } } } }
Example #10
Source File: BindingTest.java From mybatis with Apache License 2.0 | 6 votes |
@BeforeClass public static void setup() throws Exception { DataSource dataSource = BaseDataTest.createBlogDataSource(); BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL); BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("Production", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.setLazyLoadingEnabled(true); configuration.getTypeAliasRegistry().registerAlias(Blog.class); configuration.getTypeAliasRegistry().registerAlias(Post.class); configuration.getTypeAliasRegistry().registerAlias(Author.class); configuration.addMapper(BoundBlogMapper.class); configuration.addMapper(BoundAuthorMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); }
Example #11
Source File: XMLConfigBuilder.java From QuickProject with Apache License 2.0 | 6 votes |
private void environmentsElement(XNode context) throws Exception { if (context != null) { if (environment == null) { environment = context.getStringAttribute("default"); } for (XNode child : context.getChildren()) { String id = child.getStringAttribute("id"); if (isSpecifiedEnvironment(id)) { TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager")); DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource")); DataSource dataSource = dsFactory.getDataSource(); Environment.Builder environmentBuilder = new Environment.Builder(id) .transactionFactory(txFactory) .dataSource(dataSource); configuration.setEnvironment(environmentBuilder.build()); } } } }
Example #12
Source File: DefaultSqlSessionFactory.java From mybaties with Apache License 2.0 | 5 votes |
private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) { //如果没有配置事务工厂,则返回托管事务工厂 if (environment == null || environment.getTransactionFactory() == null) { return new ManagedTransactionFactory(); } return environment.getTransactionFactory(); }
Example #13
Source File: XMLConfigBuilder.java From mybaties with Apache License 2.0 | 5 votes |
private TransactionFactory transactionManagerElement(XNode context) throws Exception { if (context != null) { String type = context.getStringAttribute("type"); Properties props = context.getChildrenAsProperties(); //根据type="JDBC"解析返回适当的TransactionFactory TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance(); factory.setProperties(props); return factory; } throw new BuilderException("Environment declaration requires a TransactionFactory."); }
Example #14
Source File: Environment.java From mybaties with Apache License 2.0 | 5 votes |
public Environment(String id, TransactionFactory transactionFactory, DataSource dataSource) { if (id == null) { throw new IllegalArgumentException("Parameter 'id' must not be null"); } if (transactionFactory == null) { throw new IllegalArgumentException("Parameter 'transactionFactory' must not be null"); } this.id = id; if (dataSource == null) { throw new IllegalArgumentException("Parameter 'dataSource' must not be null"); } this.transactionFactory = transactionFactory; this.dataSource = dataSource; }
Example #15
Source File: JsonHandlersTestApi.java From mybatis-jackson with MIT License | 5 votes |
protected static SqlSessionFactory setUpDb(DataSource ds, String initSql) throws SQLException, IOException { try (final Connection cnx = ds.getConnection(); final Statement st = cnx.createStatement()) { st.execute(getResourceAsString(initSql)); } // Init mybatis TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("jneat", transactionFactory, ds); Configuration configuration = new Configuration(environment); configuration.getTypeHandlerRegistry().register("com.github.jneat.mybatis"); configuration.addMapper(JsonMapper.class); return new SqlSessionFactoryBuilder().build(configuration); }
Example #16
Source File: JsonHandlersTestApi.java From mybatis-gson with MIT License | 5 votes |
protected static SqlSessionFactory setUpDb(DataSource ds, String initSql) throws SQLException, IOException { try (final Connection cnx = ds.getConnection(); final Statement st = cnx.createStatement()) { st.execute(getResourceAsString(initSql)); } // Init mybatis TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("jneat", transactionFactory, ds); Configuration configuration = new Configuration(environment); configuration.getTypeHandlerRegistry().register("com.github.jneat.mybatis"); configuration.addMapper(JsonMapper.class); return new SqlSessionFactoryBuilder().build(configuration); }
Example #17
Source File: ResultLoader.java From mybaties with Apache License 2.0 | 5 votes |
private Executor newExecutor() { final Environment environment = configuration.getEnvironment(); if (environment == null) { throw new ExecutorException("ResultLoader could not load lazily. Environment was not configured."); } final DataSource ds = environment.getDataSource(); if (ds == null) { throw new ExecutorException("ResultLoader could not load lazily. DataSource was not configured."); } final TransactionFactory transactionFactory = environment.getTransactionFactory(); final Transaction tx = transactionFactory.newTransaction(ds, null, false); //如果executor已经被关闭了,则创建一个新的SimpleExecutor return configuration.newExecutor(tx, ExecutorType.SIMPLE); }
Example #18
Source File: ManagedTransactionFactoryTest.java From mybaties with Apache License 2.0 | 5 votes |
@Test public void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnections() throws Exception { TransactionFactory tf = new ManagedTransactionFactory(); tf.setProperties(new Properties()); Transaction tx = tf.newTransaction(conn); assertEquals(conn, tx.getConnection()); tx.commit(); tx.rollback(); tx.close(); verify(conn).close(); }
Example #19
Source File: TestSuite.java From mybatis-types with MIT License | 5 votes |
static synchronized void setupSessionFactoryBuilder(DataSource ds) { TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("jneat", transactionFactory, ds); Configuration configuration = new Configuration(environment); configuration.getTypeHandlerRegistry().register("com.github.jneat.mybatis"); configuration.setMapUnderscoreToCamelCase(true); // Add Mappers configuration.addMapper(TypesMapper.class); configuration.addMapper(ArraysMapper.class); configuration.addMapper(TimeMapper.class); ssf = new SqlSessionFactoryBuilder().build(configuration); }
Example #20
Source File: ManagedTransactionFactoryTest.java From mybaties with Apache License 2.0 | 5 votes |
@Test public void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnectionsAndDoesNotCloseConnection() throws Exception { TransactionFactory tf = new ManagedTransactionFactory(); Properties props = new Properties(); props.setProperty("closeConnection", "false"); tf.setProperties(props); Transaction tx = tf.newTransaction(conn); assertEquals(conn, tx.getConnection()); tx.commit(); tx.rollback(); tx.close(); verifyNoMoreInteractions(conn); }
Example #21
Source File: DefaultSqlSessionFactory.java From mybatis with Apache License 2.0 | 5 votes |
private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) { //如果没有配置事务工厂,则返回托管事务工厂 if (environment == null || environment.getTransactionFactory() == null) { return new ManagedTransactionFactory(); } return environment.getTransactionFactory(); }
Example #22
Source File: SqlSessionTemplateITBase.java From pinpoint with Apache License 2.0 | 5 votes |
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); Configuration configuration = mock(Configuration.class); TransactionFactory transactionFactory = mock(TransactionFactory.class); DataSource dataSource = mock(DataSource.class); Environment environment = new Environment("test", transactionFactory, dataSource); when(configuration.getEnvironment()).thenReturn(environment); when(this.sqlSessionFactory.getConfiguration()).thenReturn(configuration); when(this.sqlSessionFactory.openSession(EXECUTOR_TYPE)).thenReturn(this.sqlSessionProxy); this.sqlSessionTemplate = new SqlSessionTemplate(this.sqlSessionFactory, EXECUTOR_TYPE); }
Example #23
Source File: Environment.java From mybatis with Apache License 2.0 | 5 votes |
public Environment(String id, TransactionFactory transactionFactory, DataSource dataSource) { if (id == null) { throw new IllegalArgumentException("Parameter 'id' must not be null"); } if (transactionFactory == null) { throw new IllegalArgumentException("Parameter 'transactionFactory' must not be null"); } this.id = id; if (dataSource == null) { throw new IllegalArgumentException("Parameter 'dataSource' must not be null"); } this.transactionFactory = transactionFactory; this.dataSource = dataSource; }
Example #24
Source File: AbstractGeometryTypeHandlerTest.java From mybatis-typehandlers-postgis with Do What The F*ck You Want To Public License | 5 votes |
public static void setupSqlSessionFactory(String initSqlPath) throws Exception { DataSource dataSource = BaseDataTest.createUnpooledDataSource("jdbc.properties"); BaseDataTest.runScript(dataSource, initSqlPath); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("Production", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); }
Example #25
Source File: HierarchicalXMLConfigBuilder.java From alfresco-repository with GNU Lesser General Public License v3.0 | 5 votes |
private TransactionFactory transactionManagerElement(XNode context) throws Exception { if (context != null) { String type = context.getStringAttribute("type"); Properties props = context.getChildrenAsProperties(); TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance(); factory.setProperties(props); return factory; } throw new BuilderException("Environment declaration requires a TransactionFactory."); }
Example #26
Source File: XMLConfigBuilder.java From mybatis with Apache License 2.0 | 5 votes |
private TransactionFactory transactionManagerElement(XNode context) throws Exception { if (context != null) { String type = context.getStringAttribute("type"); Properties props = context.getChildrenAsProperties(); //根据type="JDBC"解析返回适当的TransactionFactory TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance(); factory.setProperties(props); return factory; } throw new BuilderException("Environment declaration requires a TransactionFactory."); }
Example #27
Source File: ResultLoader.java From mybatis with Apache License 2.0 | 5 votes |
private Executor newExecutor() { final Environment environment = configuration.getEnvironment(); if (environment == null) { throw new ExecutorException("ResultLoader could not load lazily. Environment was not configured."); } final DataSource ds = environment.getDataSource(); if (ds == null) { throw new ExecutorException("ResultLoader could not load lazily. DataSource was not configured."); } final TransactionFactory transactionFactory = environment.getTransactionFactory(); final Transaction tx = transactionFactory.newTransaction(ds, null, false); //如果executor已经被关闭了,则创建一个新的SimpleExecutor return configuration.newExecutor(tx, ExecutorType.SIMPLE); }
Example #28
Source File: IDAllocDaoImpl.java From Leaf with Apache License 2.0 | 5 votes |
public IDAllocDaoImpl(DataSource dataSource) { TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.addMapper(IDAllocMapper.class); sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); }
Example #29
Source File: Utils.java From ClosureTableCateogryStore with MIT License | 5 votes |
public static SqlSession createSqlSession(DataSource dataSource) { TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("test", transactionFactory, dataSource); Configuration config = new Configuration(); config.setCacheEnabled(false); config.addMapper(CategoryMapper.class); config.setEnvironment(environment); SqlSessionFactory sessionFactory = new DefaultSqlSessionFactory(config); return sessionFactory.openSession(); }
Example #30
Source File: ManagedTransactionFactoryTest.java From mybatis with Apache License 2.0 | 5 votes |
@Test public void shouldEnsureThatCallsToManagedTransactionAPIDoNotForwardToManagedConnections() throws Exception { TransactionFactory tf = new ManagedTransactionFactory(); tf.setProperties(new Properties()); Transaction tx = tf.newTransaction(conn); assertEquals(conn, tx.getConnection()); tx.commit(); tx.rollback(); tx.close(); verify(conn).close(); }