对mybatis不太熟悉。
我用的是jsp,spring+mybatis架构,数据库是oracle
请问mybatis怎样配置支持大概200个同时访问?会不会超过数据库的最大连接数?
我的服务器用tomcat行不行?
如果每天点击次数大概在10万次,要注意哪些方面?
请各位大神帮帮忙,解决了20分,非常感谢!
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);
}
tomcat200个是没问题的。
表设计的好点,一切都ok。追问
只有一张表。
下面是我mybatis的配置,能不能帮我看看?谢谢!
另外,sqlSessionFactory这个对象是每次连接都产生一个吗?
sqlSessionFactory系统初始化就生成了,不是每次连接都产生。
maxIdle:连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负数表示不限制,默认为8个,maxIdle不能设置太小,高负载的情况下,连接的打开时间比关闭的时间快,会引起连接池中idle的个数
上升超过maxIdle,而造成频繁的连接销毁和创建。
minIdle:连接池中最小的空闲的连接数,低于这个数量会被创建新的连接默认为0,该参数越接近maxIdle,性能越好,因为
连接的创建和销毁,都是需要消耗资源的;但是不能太大,因为在机器很空闲的时候,也会创建低于minidle个数的连接。
谢谢!
还有点疑问向你请教:如果连接达到了设置的最大连接(maxActive),那如果有新的请求会怎样?如果达到了最大连接,最小空闲(minIdle)不为0的话,会向数据库建立新的连接吗?另外,如果没有达到最大连接,但是我的数据库此时连接已经占满,如果再请求的话数据库或者程序会崩溃吗?
达到最大连接,再有新请求就会等待,不再创建新连接,有连接释放后再使用。
如果最大连接比数据库连接还大,那就看数据库的配置如何了,到达数据库最大连接之后,有数据库的配置控制。
了解了,非常感谢!
本回答被提问者采纳