我试图记录当前活动连接的数量。我正在com.zaxxer.hikari.HikariJNDIFactory用作我的数据源工厂。
final Context context = new InitialContext();
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDataSource((DataSource) ((Context)context.lookup("java:comp/env")).lookup("jdbc/mydb"));
HikariPool hikariPool = new HikariPool(hikariConfig);
LOGGER.log(Level.INFO, "The count is ::" + hikariPool.getActiveConnections());
Run Code Online (Sandbox Code Playgroud)
但这引发了以下异常:
java.lang.RuntimeException: java.lang.NullPointerException
at com.zaxxer.hikari.util.PoolUtilities.createInstance(PoolUtilities.java:105)
at com.zaxxer.hikari.metrics.MetricsFactory.createMetricsTracker(MetricsFactory.java:34)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:131)
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:99)
at com.something.servlet.HikariConnectionCount.doGet(HikariConnectionCount.java:35)
Run Code Online (Sandbox Code Playgroud)
HikariConnectionCount.java我写的文件在哪里
Stopwatch stopwatch1 = new Stopwatch();
Stopwatch stopwatch2 = new Stopwatch();
string l = "my test";
string u = "MY TEST";
for (int i = 0; i < 25; i++)
{
l += l;
u += u;
}
stopwatch1.Start();
l=l.ToUpper();
stopwatch1.Stop();
stopwatch2.Start();
u=u.ToLower();
stopwatch2.Stop();
// Write result.
Console.WriteLine("Time elapsed: \nUPPER : {0}\n LOWER : {1}",
stopwatch1.Elapsed, stopwatch2.Elapsed);
Run Code Online (Sandbox Code Playgroud)
我跑了很多次:
UPPER : 00:00:01.3386287
LOWER : 00:00:01.4546552
UPPER : 00:00:01.1614189
LOWER : 00:00:01.1970368
UPPER : 00:00:01.2697430
LOWER : 00:00:01.3460950
UPPER : 00:00:01.2256813
LOWER : …Run Code Online (Sandbox Code Playgroud) 在这些初始化语句编译的前提下
List<int> l = new List<int> { 1, 2, 3 };
Dictionary<int, int> d = new Dictionary<int, int> { [1] = 11, [2] = 22 };
Foo f = new Foo { Bar = new List<int>() };
Run Code Online (Sandbox Code Playgroud)
这不会
List<int> l = { 1, 2, 3 };
Dictionary<int, int> d = { [1] = 11, [2] = 22 };
Foo f = { Bar = new List<int>() };
Run Code Online (Sandbox Code Playgroud)
我有一个关于嵌套初始化的问题.鉴于以下课程
public class Foo {
public List<int> Bar { get; set; } = …Run Code Online (Sandbox Code Playgroud) 使用FW 4.5.1我有一个方法:
void DoSomething(string s)
Run Code Online (Sandbox Code Playgroud)
我想允许函数也接受int,但是在相同的参数下.这意味着我想写:
void DoSomething(var s)
Run Code Online (Sandbox Code Playgroud)
有时s会int,有时string.
我怎样才能做到这一点?
基本上如标题所述,如果我这样做:
private const string TYPEOF_STRING = typeof(String).FullName;
Run Code Online (Sandbox Code Playgroud)
为什么它会给我错误:
分配给'Cognitronics.Generic.CloudClient.TYPEOF_STRING'的表达式必须是常量
编辑:
看来我们在这里和Habib联系的线程中也有相互矛盾的答案.这里每个人都说它不是编译时间常数,而在另一个线程中每个人都说它是.这让我更加困惑,所以我想重新问一下这是什么?
是否可以使用new nameof运算符获取setter方法名称?
public object Foo { get; set; }
public void Test()
{
var myMethod = GetType().GetMethod("set_Foo");
}
Run Code Online (Sandbox Code Playgroud)
我想 GetType().GetMethod("set_" + nameof(Foo))可以工作,但还有更直接的东西吗?
有人可以解释一个索引器有什么好处?
public class MyClass
{
private List<string> list = new List<string>()
public string this[int value]
{
get
{
return list[value];
}
}
public string GetValue(int value)
{
return list[value];
}
}
Run Code Online (Sandbox Code Playgroud)
使用的好处是什么:
MyClass target = new MyClass();
string value = target[0];
Run Code Online (Sandbox Code Playgroud)
对此:
MyClass target = new MyClass();
string value = target.GetValue(0);
Run Code Online (Sandbox Code Playgroud) 我正在使用Robot打HTTP服务。但这向我展示了以下问题
找不到名称为“创建会话”的关键字。
导入测试库'RequestsLibrary'失败:ImportError:没有名为RequestsLibrary Traceback的模块(最近一次调用):
我已经安装了RequestsLibrary。我的TC是:
*** Settings ***
Library Collections
Library String
#Library RequestsLibrary
Library OperatingSystem
Library ExtendedRequestsLibrary
Suite Teardown Delete All Sessions
*** Test Cases ***
Get Requests
[Tags] get
Create Session google http://www.google.com
# Create Session github https://api.github.com
${resp}= Get google /
Should Be Equal As Strings ${resp.status_code} 200
${resp}= Get github /users/bulkan
Should Be Equal As Strings ${resp.status_code} 200
Dictionary Should Contain Value ${resp.json()} Bulkan Evcimen
Run Code Online (Sandbox Code Playgroud) 如何使用类型推断解决此问题?
open Microsoft.FSharp.Linq.RuntimeHelpers
open System
open System.Linq.Expressions
open Accord.Math.Optimization
module Vector=
let compareScalarProcut(a:double[])(b:double[])(greaterOrEqualThen:float)=
Array.map2 (*) a b
|> Array.sum
|> fun x-> x >= greaterOrEqualThen
module Lambda =
let toExpression (``f# lambda`` : Quotations.Expr<'a>) =
``f# lambda``
|> LeafExpressionConverter.QuotationToExpression
|> unbox<Expression<'a>>
let LambdaExpression (coefficients:double[]) =
<@ Func<double[], bool>(fun i -> (Vector.compareScalarProcut(i,coefficients,0)) @>
|> Lambda.toExpression
Run Code Online (Sandbox Code Playgroud)
我有以下课程:
public class MyClass
{
public Action SomeAction { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在调用c#-6.0之前,SomeAction因为它有可能是null我们会做的事情:
var action = SomeAction;
if (action != null)
{
action();
}
Run Code Online (Sandbox Code Playgroud)
但是,在c#-6.0中,我们现在有了null条件运算符,因此可以将上面的内容写成:
SomeAction?.Invoke();
Run Code Online (Sandbox Code Playgroud)
但是我发现由于这个Invoke电话,它的可读性稍差.无论如何在没有Invoke调用的情况下在这种情况下使用null条件运算符?就像是:
SomeAction?();
Run Code Online (Sandbox Code Playgroud)