List of usage examples for org.apache.ibatis.mapping Environment getDataSource
public DataSource getDataSource()
From source file:cc.oit.dao.impl.mybatis.session.XMLConfigBuilder.java
License:Apache License
private void databaseIdProviderElement(XNode context) throws Exception { DatabaseIdProvider databaseIdProvider = null; if (context != null) { String type = context.getStringAttribute("type"); if ("VENDOR".equals(type)) type = "DB_VENDOR"; // awful patch to keep backward compatibility Properties properties = context.getChildrenAsProperties(); databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance(); databaseIdProvider.setProperties(properties); }/* w w w.j a v a 2s.com*/ Environment environment = configuration.getEnvironment(); if (environment != null && databaseIdProvider != null) { String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource()); configuration.setDatabaseId(databaseId); } }
From source file:com.baomidou.mybatisplus.MybatisXMLConfigBuilder.java
License:Apache License
private void databaseIdProviderElement(XNode context) throws Exception { DatabaseIdProvider databaseIdProvider = null; if (context != null) { String type = context.getStringAttribute("type"); // awful patch to keep backward compatibility if ("VENDOR".equals(type)) { type = "DB_VENDOR"; }/* www . j av a2s .co m*/ Properties properties = context.getChildrenAsProperties(); databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance(); databaseIdProvider.setProperties(properties); } Environment environment = configuration.getEnvironment(); if (environment != null && databaseIdProvider != null) { String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource()); configuration.setDatabaseId(databaseId); } }
From source file:com.bibisco.manager.ProjectManager.java
License:GNU General Public License
public static void closeConnection() { mLog.debug("Start closeConnection()"); SqlSessionFactory lSqlSessionFactory = SqlSessionFactoryManager.getInstance().getSqlSessionFactoryProject(); Configuration lConfiguration = lSqlSessionFactory.getConfiguration(); Environment lEnvironment = lConfiguration.getEnvironment(); DataSource lDataSource = lEnvironment.getDataSource(); //production/*w w w.j av a2 s . c o m*/ if (lDataSource instanceof PooledDataSource) { ((PooledDataSource) lDataSource).forceCloseAll(); } //test else if (lDataSource instanceof JdbcDataSource) { try { lDataSource.getConnection().close(); } catch (Throwable t) { mLog.error(t); throw new BibiscoException(t, BibiscoException.SQL_EXCEPTION); } } mLog.debug("End closeConnection()"); }
From source file:com.raycloud.cobarclient.mybatis.spring.SqlSessionUtils.java
License:Apache License
/** * Gets an SqlSession from Spring Transaction Manager or creates a new one if needed. * Tries to get a SqlSession out of current transaction. If there is not any, it creates a new one. * Then, it synchronizes the SqlSession with the transaction if Spring TX is active and * <code>SpringManagedTransactionFactory</code> is configured as a transaction manager. * * @param sessionFactory a MyBatis {@code SqlSessionFactory} to create new sessions * @param executorType The executor type of the SqlSession to create * @param exceptionTranslator Optional. Translates SqlSession.commit() exceptions to Spring exceptions. * @throws TransientDataAccessResourceException if a transaction is active and the * {@code SqlSessionFactory} is not using a {@code SpringManagedTransactionFactory} * @see SpringManagedTransactionFactory/*from w ww. j a v a 2s . c o m*/ */ public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) { notNull(sessionFactory, "No SqlSessionFactory specified"); notNull(executorType, "No ExecutorType specified"); SqlSessionHolder holder = (SqlSessionHolder) getResource(sessionFactory); if (holder != null && holder.isSynchronizedWithTransaction()) { if (holder.getExecutorType() != executorType) { throw new TransientDataAccessResourceException( "Cannot change the ExecutorType when there is an existing transaction"); } holder.requested(); if (logger.isDebugEnabled()) { logger.debug("Fetched SqlSession [" + holder.getSqlSession() + "] from current transaction"); } return holder.getSqlSession(); } if (logger.isDebugEnabled()) { logger.debug("Creating a new SqlSession"); } SqlSession session = sessionFactory.openSession(executorType); // Register session holder if synchronization is active (i.e. a Spring TX is active) // // Note: The DataSource used by the Environment should be synchronized with the // transaction either through DataSourceTxMgr or another tx synchronization. // Further assume that if an exception is thrown, whatever started the transaction will // handle closing / rolling back the Connection associated with the SqlSession. if (isSynchronizationActive()) { Environment environment = sessionFactory.getConfiguration().getEnvironment(); if (environment.getTransactionFactory() instanceof SpringManagedTransactionFactory) { if (logger.isDebugEnabled()) { logger.debug("Registering transaction synchronization for SqlSession [" + session + "]"); } holder = new SqlSessionHolder(session, executorType, exceptionTranslator); bindResource(sessionFactory, holder); registerSynchronization(new SqlSessionSynchronization(holder, sessionFactory)); holder.setSynchronizedWithTransaction(true); holder.requested(); } else { if (getResource(environment.getDataSource()) == null) { if (logger.isDebugEnabled()) { logger.debug("SqlSession [" + session + "] was not registered for synchronization because DataSource is not transactional"); } } else { throw new TransientDataAccessResourceException( "SqlSessionFactory must be using a SpringManagedTransactionFactory in order to use Spring transaction synchronization"); } } } else { if (logger.isDebugEnabled()) { logger.debug("SqlSession [" + session + "] was not registered for synchronization because synchronization is not active"); } } return session; }
From source file:com.raycloud.cobarclient.mybatis.spring.SqlSessionUtils.java
License:Apache License
/** * Environment?/*from www .j av a2 s . co m*/ * * @param sessionFactory * @param executorType * @param exceptionTranslator * @param environment * @return */ public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator, Environment environment, SqlSessionHolder holder) { notNull(sessionFactory, "No SqlSessionFactory specified"); notNull(executorType, "No ExecutorType specified"); holder = (holder == null) ? (SqlSessionHolder) getResource(sessionFactory) : holder; if (holder != null && holder.isSynchronizedWithTransaction()) { if (holder.getExecutorType() != executorType) { throw new TransientDataAccessResourceException( "Cannot change the ExecutorType when there is an existing transaction"); } holder.requested(); if (logger.isDebugEnabled()) { logger.debug("Fetched SqlSession [" + holder.getSqlSession() + "] from current transaction"); } return holder.getSqlSession(); } if (logger.isDebugEnabled()) { logger.debug("Creating a new SqlSession"); } SqlSession session = /**sessionFactory.openSession(executorType);**/ openSession(environment, sessionFactory, executorType); // Register session holder if synchronization is active (i.e. a Spring TX is active) // // Note: The DataSource used by the Environment should be synchronized with the // transaction either through DataSourceTxMgr or another tx synchronization. // Further assume that if an exception is thrown, whatever started the transaction will // handle closing / rolling back the Connection associated with the SqlSession. if (isSynchronizationActive()) { // Environment environment = sessionFactory.getConfiguration().getEnvironment(); if (environment.getTransactionFactory() instanceof SpringManagedTransactionFactory) { if (logger.isDebugEnabled()) { logger.debug("Registering transaction synchronization for SqlSession [" + session + "]"); } holder = new SqlSessionHolder(session, executorType, exceptionTranslator); bindResource(sessionFactory, holder); registerSynchronization(new SqlSessionSynchronization(holder, sessionFactory)); holder.setSynchronizedWithTransaction(true); holder.requested(); } else { if (getResource(environment.getDataSource()) == null) { if (logger.isDebugEnabled()) { logger.debug("SqlSession [" + session + "] was not registered for synchronization because DataSource is not transactional"); } } else { throw new TransientDataAccessResourceException( "SqlSessionFactory must be using a SpringManagedTransactionFactory in order to use Spring transaction synchronization"); } } } else { if (logger.isDebugEnabled()) { logger.debug("SqlSession [" + session + "] was not registered for synchronization because synchronization is not active"); } } return session; }
From source file:com.raycloud.cobarclient.mybatis.spring.SqlSessionUtils.java
License:Apache License
/** * Environment?//ww w. j a v a 2 s . co m * * @param environment * @param sessionFactory * @param executorType * @return */ private static SqlSession openSession(Environment environment, SqlSessionFactory sessionFactory, ExecutorType executorType) { Transaction tx = null; try { final TransactionFactory transactionFactory = environment.getTransactionFactory(); tx = transactionFactory.newTransaction(environment.getDataSource(), null, false); final Executor executor = sessionFactory.getConfiguration().newExecutor(tx, executorType); return new DefaultSqlSession(sessionFactory.getConfiguration(), executor, false); } 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(); } }
From source file:myguice.SampleBasicTest.java
License:Apache License
@Before public void setupMyBatisGuice() throws Exception { // bindings// w w w . ja v a2 s .c o m this.injector = createInjector(new MyBatisModule() { @Override protected void initialize() { install(JdbcHelper.HSQLDB_IN_MEMORY_NAMED); bindDataSourceProviderType(PooledDataSourceProvider.class); bindTransactionFactoryType(JdbcTransactionFactory.class); addMapperClass(UserMapper.class); bindProperties(binder(), createTestProperties()); bind(FooService.class).to(FooServiceMapperImpl.class); bind(UserDao.class).to(UserDaoImpl.class); } }); // prepare the test db Environment environment = this.injector.getInstance(SqlSessionFactory.class).getConfiguration() .getEnvironment(); DataSource dataSource = environment.getDataSource(); ScriptRunner runner = new ScriptRunner(dataSource.getConnection()); runner.setAutoCommit(true); runner.setStopOnError(true); runner.runScript(getResourceAsReader("sample/db/database-schema.sql")); runner.runScript(getResourceAsReader("sample/db/database-test-data.sql")); runner.closeConnection(); this.fooService = this.injector.getInstance(FooService.class); }
From source file:och.comp.db.base.BaseDb.java
License:Apache License
private CommitOnCloseSession openCommitOnCloseSession(boolean batch) { ExecutorType executorType = batch ? ExecutorType.BATCH : ExecutorType.SIMPLE; if (!isSingleTxMode()) { return new CommitOnCloseSession(sessionFactory.openSession(executorType)); }//www . ja v a 2 s . c o m //SINGLE CONN MODE Environment env = sessionFactory.getConfiguration().getEnvironment(); DataSource ds = env.getDataSource(); Connection conn = null; try { conn = getSingleOrNewConnection(ds); } catch (Exception e) { throw new IllegalStateException("can't get conneciton", e); } return new CommitOnCloseSession(sessionFactory.openSession(executorType, conn)); }
From source file:org.activiti.MultiTenantSqlSessionFactory.java
License:Apache License
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null;/*w ww . j a v a 2 s . c o m*/ try { final Environment environment = getConfiguration().getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); final Executor executor = getConfiguration().newExecutor(tx, execType, autoCommit); return new DefaultSqlSession(getConfiguration(), executor); } 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(); } }
From source file:org.alfresco.ibatis.HierarchicalXMLConfigBuilder.java
License:Open Source License
private void databaseIdProviderElement(XNode context) throws Exception { DatabaseIdProvider databaseIdProvider = null; if (context != null) { String type = context.getStringAttribute("type"); Properties properties = context.getChildrenAsProperties(); databaseIdProvider = (DatabaseIdProvider) resolveClass(type).newInstance(); databaseIdProvider.setProperties(properties); }/*from w w w . j a v a 2 s . c om*/ Environment environment = configuration.getEnvironment(); if (environment != null && databaseIdProvider != null) { String databaseId = databaseIdProvider.getDatabaseId(environment.getDataSource()); configuration.setDatabaseId(databaseId); } }