在一段时间不活动后,我在 public class HomeScreenActivity extends AppCompatActivity {
AlertDialog alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
overridePendingTransition(R.anim.trans_left_in, R.anim.trans_left_out);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
}
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSION_REQUEST_COARSE_LOCATION);
}
}
}
myBleRecever = new BleBroadCastReceiver();
registerReceiver(myBleRecever, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
if(device_macid=="")
{
show_dialog();
}
}
private void show_dialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(HomeScreenActivity.this);
builder.setMessage(R.string.connect_device)
.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(HomeScreenActivity.this, BLEdevicelistActivity.class);
if (isBleseviceRegister) {
unregisterReceiver(mGattUpdateReceiver);
isBleseviceRegister = false;
}
if (isBindServise) {
unbindService(mServiceConnection);
isBindServise = false;
}
alert.dismiss();
startActivityForResult(intent, Constants.BLUETOOTH_REQUEST_CODE);
}
});
alert = builder.create();
// alert.setCancelable(false);
alert.setCanceledOnTouchOutside(false);
Logger.log(Level.DEBUG,TAG,"Alert dialog box gets called");
alert.show();// **getting error here**
}
调用(默认Java驱动程序)上收到此错误。我尝试添加手动心跳(写入上限集合),但它没有帮助。我只是在连接到compose上的实例时(即不在本地上下文中)得到问题。
MongoDB版本是3.2.8,最新驱动程序(3.3),使用Java 8。
有什么想法吗?
答案 0 :(得分:15)
我在一些文档中找到了它:
对于长时间运行的应用程序,通常谨慎地启用“keepAlive”数毫秒。没有它,经过一段时间后,您可能会开始看到“连接已关闭”错误,这似乎是没有理由的。
检查这是否有帮助。当您连接到mongoDB时,您可以将套接字选项传递给它。我来自节点背景,我们使用以下选项让它保持活力。
server: {
socketOptions: {
keepAlive: 100,
connectTimeoutMS: 30000
}
}
希望这会有所帮助!!
答案 1 :(得分:7)
我通过将sslEnabled设置为true来解决此问题,代码示例:
@Bean
public MongoClient mongoClient() {
List<ServerAddress> saList = new ArrayList<>();
saList.add(new ServerAddress("cluster0-shard-00-00-75shm.gcp.mongodb.net", 27017));
saList.add(new ServerAddress("cluster0-shard-00-01-75shm.gcp.mongodb.net", 27017));
saList.add(new ServerAddress("cluster0-shard-00-02-75shm.gcp.mongodb.net", 27017));
char[] pwd = "password".toCharArray();
MongoCredential credential = MongoCredential.createCredential("username", "admin", pwd);
//set sslEnabled to true here
MongoClientOptions options = MongoClientOptions.builder()
.readPreference(ReadPreference.primaryPreferred())
.retryWrites(true)
.requiredReplicaSetName("Cluster0-shard-0")
.maxConnectionIdleTime(6000)
.sslEnabled(true)
.build();
MongoClient mongoClient = new MongoClient(saList, credential, options);
return mongoClient;
}
另外:我的客户jar是org.mongodb.mongodb-driver 3.6.4,服务器是GCP上的mongodb atlas M0 3.6.6
答案 2 :(得分:2)
我同意Rhangaun的答案,这是我在JAVA代码中的解决方案:
public static DB getMongoDB() {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
//build the connection options
builder.maxConnectionIdleTime(60000);//set the max wait time in (ms)
MongoClientOptions opts = builder.build();
char[] password2 = "mypassword".toCharArray();
MongoCredential credential2 = MongoCredential.createCredential("username", "databasename",password2);
//add your option to the connection
MongoClient mongoClient = new MongoClient(new ServerAddress("server ip",27017), Arrays.asList(credential2),opts);
//use your database
cachedDb = mongoClient.getDB("databasename");
return cachedDb;
}
以下是我的研究链接:http://3t.io/blog/how-to-prevent-your-connection-from-dropping-with-hosted-mongodb-instances/
希望它对你有所帮助。
答案 3 :(得分:2)
像这样编辑application.properties:
spring.data.mongodb.uri = mongodb+srv://username:password@solarsystem-1tpu0.mongodb.net/dbname
对于常规mongo实例(非集群),请使用以下命令:
spring.data.mongodb.uri = mongodb://username:password@solarsystem-shard-00-00-1tpu0.mongodb.net:27017,hostname2:27017/dbname?ssl=true
答案 4 :(得分:0)
对我来说,这是一个完全不同的问题-我在mongo-java-server中使用Fongo并最终遇到此错误。原来是old versions of it are not compatible with FieldType.DECIMAL128
conversions。
将其更新到最新版本(当前为1.36.0)为我解决了该问题。
答案 5 :(得分:0)
问题是Mongodb终止了连接。您需要增加Mongodb驱动程序的超时,这是示例代码。
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
//build the connection options
builder.maxConnectionIdleTime(86400000);//set the max wait time in (ms)
MongoClientOptions opts = builder.build();
final Morphia morphia = new Morphia();
morphia.mapPackage("com.java.code");
final String hostURL = "host_url";
MongoCredential credential = MongoCredential.createCredential("username","database","Password".toCharArray());
ServerAddress address = new ServerAddress(hostURL);
List<MongoCredential> credentialList = new ArrayList<>();
credentialList.add(credential);
final MongoClient client = new MongoClient(address,credentialList,opts);
// create the Datastore connecting to the default port on the local host
datastore = morphia.createDatastore(client,"datastore");