[20分] mybatis怎样配置支持并发

对mybatis不太熟悉。我用的是jsp,spring+mybatis架构,数据库是oracle请问mybatis怎样配置支持大概200个同时访问?会不会超过数据库的最大... 对mybatis不太熟悉。
我用的是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);
  }
网络首席喷子
推荐于2016-10-21 · TA获得超过122个赞
知道答主
回答量:129
采纳率:0%
帮助的人:67.9万
展开全部
mybatis数据库连接池的配置maxActive配大一些,initialSize也稍加一些。
tomcat200个是没问题的。

表设计的好点,一切都ok。
更多追问追答
追问

只有一张表。

下面是我mybatis的配置,能不能帮我看看?谢谢!

另外,sqlSessionFactory这个对象是每次连接都产生一个吗?

追答
sqlSessionFactory系统初始化就生成了,不是每次连接都产生。

maxIdle:连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负数表示不限制,默认为8个,maxIdle不能设置太小,高负载的情况下,连接的打开时间比关闭的时间快,会引起连接池中idle的个数
上升超过maxIdle,而造成频繁的连接销毁和创建。
minIdle:连接池中最小的空闲的连接数,低于这个数量会被创建新的连接默认为0,该参数越接近maxIdle,性能越好,因为
连接的创建和销毁,都是需要消耗资源的;但是不能太大,因为在机器很空闲的时候,也会创建低于minidle个数的连接。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式