本文整理匯總了Java中org.apache.ibatis.transaction.TransactionFactory類的典型用法代碼示例。如果您正苦於以下問題:Java TransactionFactory類的具體用法?Java TransactionFactory怎麽用?Java TransactionFactory使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TransactionFactory類屬於org.apache.ibatis.transaction包,在下文中一共展示了TransactionFactory類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: environmentsElement
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
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());
}
}
}
}
示例2: DBManager
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
public DBManager(AppConfiguration appConfig) {
this.appConfig = appConfig;
boolean isDev = appConfig.getBool(CONFIG_KEY_APP_DEV_MODE, true);
String dbHost = appConfig.get(CONFIG_KEY_MYSQL_HOST, DEFAULT_MYSQL_HOST);
int dbPort = appConfig.getInt(CONFIG_KEY_MYSQL_PORT, DEFAULT_MYSQL_PORT);
String dbName = appConfig.get(CONFIG_KEY_MYSQL_DBNAME, DEFAULT_MEYSQL_DBNAME);
String dbUser = appConfig.get(CONFIG_KEY_MYSQL_USER, "");
String dbPass = appConfig.get(CONFIG_KEY_MYSQL_PASS, "");
String dbDriver = "com.mysql.cj.jdbc.Driver";
String connString = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName + "?autoReconnect=true&useSSL=false&useUnicode=true";
DataSource dataSource = new PooledDataSource(dbDriver, connString, dbUser, dbPass);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment(isDev ? "development" : "production", transactionFactory, dataSource);
configuration = new Configuration(environment);
configuration.addMappers("com.elune.dao");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
}
示例3: build
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
/**
* Create an instance of MyBatis.
*
* @param environment The dropwizard environment
* @param config A Mybatis config object
* @param dataSource
* @param name The name of this mybatis factory used for metrics
* @return An instance of MyBatis.
*/
public final SqlSessionFactory build(Environment environment,
MyBatisConfiguration config,
ManagedDataSource dataSource,
String name) {
SqlSessionFactory sessionFactory = null;
// Try to use the mybatis configuration file if it is specified and exists.
try (InputStream inputStream = Resources.getResourceAsStream(config.getConfigFile())) {
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException ioe) {
// Build session factory from configuration values given in the dropwizard config.
TransactionFactory transactionFactory = new JdbcTransactionFactory();
org.apache.ibatis.mapping.Environment myBatisEnvironment =
new org.apache.ibatis.mapping.Environment(ENV_NAME, transactionFactory, dataSource);
Configuration mybatisConfiguration = new Configuration(myBatisEnvironment);
sessionFactory = new SqlSessionFactoryBuilder().build(mybatisConfiguration);
}
environment.lifecycle().manage(dataSource);
environment.healthChecks().register(name,
new MyBatisHealthCheck(sessionFactory, config.getConfig().getValidationQuery()));
return sessionFactory;
}
示例4: init
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
@Override
public void init(DbConnection connection) throws Exception {
JdbcConnectionPool pool = JdbcConnectionPool.create(connection.uri, connection.username, connection.password);
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("jdblender", transactionFactory, pool);
Configuration cfg = new Configuration(environment);
cfg.setMapUnderscoreToCamelCase(true);
cfg.addMapper(BrandsMapper.class);
cfg.addMapper(SeriesMapper.class);
cfg.addMapper(ModelsMapper.class);
cfg.addMapper(SparesMapper.class);
//Create session
sqlSessionFactory = new SqlSessionFactoryBuilder().build(cfg);
}
示例5: environmentsElement
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
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());
}
}
}
}
示例6: setup
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
@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.setUseActualParamName(false); // to test legacy style reference (#{0} #{1})
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);
}
示例7: openSessionFromDataSource
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
@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();
}
}
示例8: openSessionFromDataSource
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
/**
* 源碼解析: 從數據源創建會話
*
* @param execType 執行類型
* @param level 數據庫隔離級別
* @param autoCommit 是否自動提交
* @return
*/
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);
// 源碼解析: 創建一個新的sql會話
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();
}
}
示例9: setup
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
@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);
}
示例10: environmentsElement
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
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());
}
}
}
}
示例11: openSessionFromDataSource
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
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();
}
}
示例12: environmentsElement
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
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());
}
}
}
}
示例13: BaseDb
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
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);
}
示例14: environmentsElement
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
/**
* 解析environments節點,並將結果設置到Configuration對象中
* 注意:創建envronment時,如果SqlSessionFactoryBuilder指定了特定的環境(即數據源); 則返回指定環境(數據源)的Environment對象,否則返回默認的Environment對象;
* 這種方式實現了MyBatis可以連接多數據源
* Builder模式應用2: 數據庫連接環境Environment對象的創建
* @param context
* @throws Exception
*/
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)) { //是和默認的環境相同時,解析之
//1.創建事務工廠 TransactionFactory
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
//2.創建數據源DataSource
DataSource dataSource = dsFactory.getDataSource();
//使用了Environment內置的構造器Builder,傳遞id、 事務工廠和數據源
//3. 構造Environment對象
Environment.Builder environmentBuilder = new Environment.Builder(id)
.transactionFactory(txFactory)
.dataSource(dataSource);
//4. 將創建的Envronment對象設置到configuration 對象中
configuration.setEnvironment(environmentBuilder.build());
}
}
}
}
示例15: transactionManagerElement
import org.apache.ibatis.transaction.TransactionFactory; //導入依賴的package包/類
/**
* 解析<transactionManager>節點,創建對應的TransactionFactory
* <environment>節點定義了連接某個數據庫的信息,
* 其子節點<transactionManager> 的type 會決定我們用什麽類型的事務管理機製。
* @param context
* @return
* @throws Exception
*/
private TransactionFactory transactionManagerElement(XNode context) throws Exception {
if (context != null) {
//type=JDBC,使用jdbc管理事務;type=MANAGED,使用WEB容器(Tomcat、JBOSS)管理事務
String type = context.getStringAttribute("type");
Properties props = context.getChildrenAsProperties();
/*在Configuration初始化的時候,會通過以下語句,給JDBC和MANAGED對應的工廠類
typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
下述的resolveClass(type).newInstance()會創建對應的工廠實例 */
TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance();
factory.setProperties(props);
return factory;
}
throw new BuilderException("Environment declaration requires a TransactionFactory.");
}