[20分] mybatis怎样配置支持并发
对mybatis不太熟悉。我用的是jsp,spring+mybatis架构,数据库是oracle请问mybatis怎样配置支持大概200个同时访问?会不会超过数据库的最大...
对mybatis不太熟悉。
我用的是jsp,spring+mybatis架构,数据库是oracle
请问mybatis怎样配置支持大概200个同时访问?会不会超过数据库的最大连接数?
我的服务器用tomcat行不行?
如果每天点击次数大概在10万次,要注意哪些方面?
请各位大神帮帮忙,解决了20分,非常感谢! 展开
我用的是jsp,spring+mybatis架构,数据库是oracle
请问mybatis怎样配置支持大概200个同时访问?会不会超过数据库的最大连接数?
我的服务器用tomcat行不行?
如果每天点击次数大概在10万次,要注意哪些方面?
请各位大神帮帮忙,解决了20分,非常感谢! 展开
推荐于2019-03-04
展开全部
参考配置方法如下:
1、修改SqlSessionFactoryBean,或者继承该类, 增加configLocations, 移除 configLocation
private Resource[] configLocations;
/*修改该方法*/
public void setConfigLocation(Resource configLocation){
this.configLocations = configLocation != null ? new Resource[] { configLocation } : null;
}
/*增加该方法*/
public void setConfigLocations(Resource[] configLocations) {
this.configLocations = configLocations;
}
/**
* 合并mybatis配置文件
*/
public Document SQLConfigMap()
{
Document doc = DocumentHelper.createDocument();
doc.setXMLEncoding("UTF-8");
DocumentFactory documentFactory = new DocumentFactory();
DocumentType docType = documentFactory.createDocType("configuration",
"-//mybatis.org//DTD Config 3.0//EN", "http://mybatis.org/dtd/mybatis-3-config.dtd");
doc.setDocType(docType);
Element rootElement = doc.addElement("configuration");
rootElement.addElement("typeAliases");
rootElement.addElement("mappers");
return doc;
}
public void readXML(Resource configXML, final Element elementTypeAlias,
final Element elementMapper)
{
// Document document = null;
SAXReader saxReader = new SAXReader();
// Element root = doc.getRootElement();
/*typeAliases合并*/
saxReader.addHandler("/configuration/typeAliases/typeAlias", new ElementHandler()
{
@Override
public void onEnd(ElementPath path)
{
Element row = path.getCurrent();
Element els = elementTypeAlias.addElement("typeAlias");
els.addAttribute("alias", row.attributeValue("alias")).addAttribute("type",
row.attributeValue("type"));
row.detach();
}
@Override
public void onStart(ElementPath arg0)
{
// TODO Auto-generated method stub
}
});
/*mapper合并*/
saxReader.addHandler("/configuration/mappers/mapper", new ElementHandler()
{
@Override
public void onEnd(ElementPath path)
{
Element row = path.getCurrent();
Element els = elementMapper.addElement("mapper");
String mapper = row.attributeValue("mapper");
String resource = row.attributeValue("resource");
els.addAttribute("mapper", mapper);
els.addAttribute("resource", resource);
row.detach();
}
@Override
public void onStart(ElementPath arg0)
{
// TODO Auto-generated method stub
}
});
try
{
saxReader.read(configXML.getInputStream());
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//return doc;
}
/**
* Build a {@code SqlSessionFactory} instance.
*
* The default implementation uses the standard MyBatis {@code XMLConfigBuilder}
* API to build a
* {@code SqlSessionFactory} instance based on an Reader.
*
* @return SqlSessionFactory
* @throws IOException if loading the config file failed
*/
protected SqlSessionFactory buildSqlSessionFactory() throws IOException
{
Configuration configuration = null;
XMLConfigBuilder xmlConfigBuilder = null;
Document document = this.SQLConfigMap();
Element root = document.getRootElement();
Element elementMapper = root.element("mappers");
Element elementTypeAlias = root.element("typeAliases");
for (Resource configLocation : configLocations)
{
readXML(configLocation, elementTypeAlias, elementMapper);
}
// Reader reader = null; InputStream inputStream = null;
if (this.configLocations != null)
{
logger.debug(document.asXML());
InputStream inputSteam = new ByteArrayInputStream(document.asXML().getBytes());
xmlConfigBuilder = new XMLConfigBuilder(inputSteam, null, this.configurationProperties);
configuration = xmlConfigBuilder.getConfiguration();
if (inputSteam != null)
{
inputSteam.close();
inputSteam = null;
}
document = null;
}
else
{
if (this.logger.isDebugEnabled())
{
this.logger.debug("Property 'configLocation' not specified,
using default MyBatis Configuration");
}
configuration = new Configuration();
configuration.setVariables(this.configurationProperties);
}
if (hasLength(this.typeAliasesPackage))
{
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeAliasPackageArray)
{
configuration.getTypeAliasRegistry().registerAliases(packageToScan);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Scanned package: '" + packageToScan + "' for aliases");
}
}
}
if (!isEmpty(this.typeAliases))
{
for (Class<?> typeAlias : this.typeAliases)
{
configuration.getTypeAliasRegistry().registerAlias(typeAlias);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered type alias: '" + typeAlias + "'");
}
}
}
if (!isEmpty(this.plugins))
{
for (Interceptor plugin : this.plugins)
{
configuration.addInterceptor(plugin);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered plugin: '" + plugin + "'");
}
}
}
if (hasLength(this.typeHandlersPackage))
{
String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeHandlersPackageArray)
{
configuration.getTypeHandlerRegistry().register(packageToScan);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
}
}
}
if (!isEmpty(this.typeHandlers))
{
for (TypeHandler<?> typeHandler : this.typeHandlers)
{
configuration.getTypeHandlerRegistry().register(typeHandler);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered type handler: '" + typeHandler + "'");
}
}
}
if (xmlConfigBuilder != null)
{
try
{
xmlConfigBuilder.parse();
if (this.logger.isDebugEnabled())
{
this.logger.debug("Parsed configuration file: '" + this.configLocations + "'");
}
}
catch (Exception ex)
{
throw new NestedIOException("Failed to parse config resource: " +
this.configLocations, ex);
}
finally
{
ErrorContext.instance().reset();
}
}
if (this.transactionFactory == null)
{
this.transactionFactory = new SpringManagedTransactionFactory();
}
Environment environment = new Environment(this.environment, this.transactionFactory,
this.dataSource);
configuration.setEnvironment(environment);
if (this.databaseIdProvider != null)
{
try
{
configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
}
catch (SQLException e)
{
throw new NestedIOException("Failed getting a databaseId", e);
}
}
if (!isEmpty(this.mapperLocations))
{
for (Resource mapperLocation : this.mapperLocations)
{
if (mapperLocation == null)
{
continue;
}
try
{
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
configuration, mapperLocation.toString(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
}
catch (Exception e)
{
throw new NestedIOException("Failed to parse mapping resource: '" +
mapperLocation + "'", e);
}
finally
{
ErrorContext.instance().reset();
}
if (this.logger.isDebugEnabled())
{
this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
}
}
}
else
{
if (this.logger.isDebugEnabled())
{
this.logger.debug("Property 'mapperLocations' was not specified or
no matching resources found");
}
}
return this.sqlSessionFactoryBuilder.build(configuration);
}
1、修改SqlSessionFactoryBean,或者继承该类, 增加configLocations, 移除 configLocation
private Resource[] configLocations;
/*修改该方法*/
public void setConfigLocation(Resource configLocation){
this.configLocations = configLocation != null ? new Resource[] { configLocation } : null;
}
/*增加该方法*/
public void setConfigLocations(Resource[] configLocations) {
this.configLocations = configLocations;
}
/**
* 合并mybatis配置文件
*/
public Document SQLConfigMap()
{
Document doc = DocumentHelper.createDocument();
doc.setXMLEncoding("UTF-8");
DocumentFactory documentFactory = new DocumentFactory();
DocumentType docType = documentFactory.createDocType("configuration",
"-//mybatis.org//DTD Config 3.0//EN", "http://mybatis.org/dtd/mybatis-3-config.dtd");
doc.setDocType(docType);
Element rootElement = doc.addElement("configuration");
rootElement.addElement("typeAliases");
rootElement.addElement("mappers");
return doc;
}
public void readXML(Resource configXML, final Element elementTypeAlias,
final Element elementMapper)
{
// Document document = null;
SAXReader saxReader = new SAXReader();
// Element root = doc.getRootElement();
/*typeAliases合并*/
saxReader.addHandler("/configuration/typeAliases/typeAlias", new ElementHandler()
{
@Override
public void onEnd(ElementPath path)
{
Element row = path.getCurrent();
Element els = elementTypeAlias.addElement("typeAlias");
els.addAttribute("alias", row.attributeValue("alias")).addAttribute("type",
row.attributeValue("type"));
row.detach();
}
@Override
public void onStart(ElementPath arg0)
{
// TODO Auto-generated method stub
}
});
/*mapper合并*/
saxReader.addHandler("/configuration/mappers/mapper", new ElementHandler()
{
@Override
public void onEnd(ElementPath path)
{
Element row = path.getCurrent();
Element els = elementMapper.addElement("mapper");
String mapper = row.attributeValue("mapper");
String resource = row.attributeValue("resource");
els.addAttribute("mapper", mapper);
els.addAttribute("resource", resource);
row.detach();
}
@Override
public void onStart(ElementPath arg0)
{
// TODO Auto-generated method stub
}
});
try
{
saxReader.read(configXML.getInputStream());
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//return doc;
}
/**
* Build a {@code SqlSessionFactory} instance.
*
* The default implementation uses the standard MyBatis {@code XMLConfigBuilder}
* API to build a
* {@code SqlSessionFactory} instance based on an Reader.
*
* @return SqlSessionFactory
* @throws IOException if loading the config file failed
*/
protected SqlSessionFactory buildSqlSessionFactory() throws IOException
{
Configuration configuration = null;
XMLConfigBuilder xmlConfigBuilder = null;
Document document = this.SQLConfigMap();
Element root = document.getRootElement();
Element elementMapper = root.element("mappers");
Element elementTypeAlias = root.element("typeAliases");
for (Resource configLocation : configLocations)
{
readXML(configLocation, elementTypeAlias, elementMapper);
}
// Reader reader = null; InputStream inputStream = null;
if (this.configLocations != null)
{
logger.debug(document.asXML());
InputStream inputSteam = new ByteArrayInputStream(document.asXML().getBytes());
xmlConfigBuilder = new XMLConfigBuilder(inputSteam, null, this.configurationProperties);
configuration = xmlConfigBuilder.getConfiguration();
if (inputSteam != null)
{
inputSteam.close();
inputSteam = null;
}
document = null;
}
else
{
if (this.logger.isDebugEnabled())
{
this.logger.debug("Property 'configLocation' not specified,
using default MyBatis Configuration");
}
configuration = new Configuration();
configuration.setVariables(this.configurationProperties);
}
if (hasLength(this.typeAliasesPackage))
{
String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeAliasPackageArray)
{
configuration.getTypeAliasRegistry().registerAliases(packageToScan);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Scanned package: '" + packageToScan + "' for aliases");
}
}
}
if (!isEmpty(this.typeAliases))
{
for (Class<?> typeAlias : this.typeAliases)
{
configuration.getTypeAliasRegistry().registerAlias(typeAlias);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered type alias: '" + typeAlias + "'");
}
}
}
if (!isEmpty(this.plugins))
{
for (Interceptor plugin : this.plugins)
{
configuration.addInterceptor(plugin);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered plugin: '" + plugin + "'");
}
}
}
if (hasLength(this.typeHandlersPackage))
{
String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
for (String packageToScan : typeHandlersPackageArray)
{
configuration.getTypeHandlerRegistry().register(packageToScan);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Scanned package: '" + packageToScan + "' for type handlers");
}
}
}
if (!isEmpty(this.typeHandlers))
{
for (TypeHandler<?> typeHandler : this.typeHandlers)
{
configuration.getTypeHandlerRegistry().register(typeHandler);
if (this.logger.isDebugEnabled())
{
this.logger.debug("Registered type handler: '" + typeHandler + "'");
}
}
}
if (xmlConfigBuilder != null)
{
try
{
xmlConfigBuilder.parse();
if (this.logger.isDebugEnabled())
{
this.logger.debug("Parsed configuration file: '" + this.configLocations + "'");
}
}
catch (Exception ex)
{
throw new NestedIOException("Failed to parse config resource: " +
this.configLocations, ex);
}
finally
{
ErrorContext.instance().reset();
}
}
if (this.transactionFactory == null)
{
this.transactionFactory = new SpringManagedTransactionFactory();
}
Environment environment = new Environment(this.environment, this.transactionFactory,
this.dataSource);
configuration.setEnvironment(environment);
if (this.databaseIdProvider != null)
{
try
{
configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
}
catch (SQLException e)
{
throw new NestedIOException("Failed getting a databaseId", e);
}
}
if (!isEmpty(this.mapperLocations))
{
for (Resource mapperLocation : this.mapperLocations)
{
if (mapperLocation == null)
{
continue;
}
try
{
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),
configuration, mapperLocation.toString(), configuration.getSqlFragments());
xmlMapperBuilder.parse();
}
catch (Exception e)
{
throw new NestedIOException("Failed to parse mapping resource: '" +
mapperLocation + "'", e);
}
finally
{
ErrorContext.instance().reset();
}
if (this.logger.isDebugEnabled())
{
this.logger.debug("Parsed mapper file: '" + mapperLocation + "'");
}
}
}
else
{
if (this.logger.isDebugEnabled())
{
this.logger.debug("Property 'mapperLocations' was not specified or
no matching resources found");
}
}
return this.sqlSessionFactoryBuilder.build(configuration);
}
展开全部
mybatis数据库连接池的配置maxActive配大一些,initialSize也稍加一些。
tomcat200个是没问题的。
表设计的好点,一切都ok。
tomcat200个是没问题的。
表设计的好点,一切都ok。
更多追问追答
追答
sqlSessionFactory系统初始化就生成了,不是每次连接都产生。
maxIdle:连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负数表示不限制,默认为8个,maxIdle不能设置太小,高负载的情况下,连接的打开时间比关闭的时间快,会引起连接池中idle的个数
上升超过maxIdle,而造成频繁的连接销毁和创建。
minIdle:连接池中最小的空闲的连接数,低于这个数量会被创建新的连接默认为0,该参数越接近maxIdle,性能越好,因为
连接的创建和销毁,都是需要消耗资源的;但是不能太大,因为在机器很空闲的时候,也会创建低于minidle个数的连接。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询