Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSuchElementException when execute DALStatement without USE DB #34224

Open
RaigorJiang opened this issue Jan 2, 2025 · 3 comments · May be fixed by #34401
Open

NoSuchElementException when execute DALStatement without USE DB #34224

RaigorJiang opened this issue Jan 2, 2025 · 3 comments · May be fixed by #34401

Comments

@RaigorJiang
Copy link
Contributor

RaigorJiang commented Jan 2, 2025

Bug Report

Which version of ShardingSphere did you use?

5.5.2-SNAPSHOT a6a09ba

Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?

ShardingSphere-Proxy

Expected behavior

Query from unicast database

Actual behavior

image

Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.

  1. Create logical database and register storage unit;
  2. Quit current session;
  3. Connect to Proxy and do not execute USE DB;
  4. Execute SQL
show variables where Variable_name in ('explicit_defaults_for_timestamp', 'sql_mode');
  1. Proxy log
java.util.NoSuchElementException: null
	at java.base/java.util.Collections$EmptyIterator.next(Collections.java:4310)
	at org.apache.shardingsphere.infra.session.query.QueryContext.getUsedDatabase(QueryContext.java:95)
	at org.apache.shardingsphere.proxy.backend.connector.StandardDatabaseConnector.<init>(StandardDatabaseConnector.java:126)
	at org.apache.shardingsphere.proxy.backend.connector.DatabaseConnectorFactory.newInstance(DatabaseConnectorFactory.java:52)
	at org.apache.shardingsphere.proxy.backend.handler.data.impl.UnicastDatabaseBackendHandler.execute(UnicastDatabaseBackendHandler.java:62)
	at org.apache.shardingsphere.proxy.frontend.mysql.command.query.text.query.MySQLComQueryPacketExecutor.execute(MySQLComQueryPacketExecutor.java:93)
	at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.doExecuteCommand(CommandExecutorTask.java:126)
	at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.executeCommand(CommandExecutorTask.java:121)
	at org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask.run(CommandExecutorTask.java:78)
	at com.alibaba.ttl.TtlRunnable.run(TtlRunnable.java:60)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)
@amirhmd
Copy link
Contributor

amirhmd commented Jan 15, 2025

@RaigorJiang the fix could be broader than below code:

  public ShardingSphereDatabase getUsedDatabase() {
            ShardingSpherePreconditions.checkNotEmpty(usedDatabaseNames, NoDatabaseSelectedException::new); // this line
            ShardingSpherePreconditions.checkState(usedDatabaseNames.size() <= 1,
                    () -> new UnsupportedSQLOperationException(String.format("Can not support multiple logic databases [%s]", Joiner.on(", ").join(usedDatabaseNames))));
            String databaseName = usedDatabaseNames.iterator().next();
            ShardingSpherePreconditions.checkState(metaData.containsDatabase(databaseName), () -> new UnknownDatabaseException(databaseName));
            return metaData.getDatabase(databaseName);
  }

or it should be handle earlier like ConnectionSession?

@taojintianxia taojintianxia added this to the 5.5.3 milestone Jan 16, 2025
@RaigorJiang
Copy link
Contributor Author

@RaigorJiang the fix could be broader than below code:

  public ShardingSphereDatabase getUsedDatabase() {
            ShardingSpherePreconditions.checkNotEmpty(usedDatabaseNames, NoDatabaseSelectedException::new); // this line
            ShardingSpherePreconditions.checkState(usedDatabaseNames.size() <= 1,
                    () -> new UnsupportedSQLOperationException(String.format("Can not support multiple logic databases [%s]", Joiner.on(", ").join(usedDatabaseNames))));
            String databaseName = usedDatabaseNames.iterator().next();
            ShardingSpherePreconditions.checkState(metaData.containsDatabase(databaseName), () -> new UnknownDatabaseException(databaseName));
            return metaData.getDatabase(databaseName);
  }

or it should be handle earlier like ConnectionSession?

@amirhmd In fact, usedDatabaseName can indeed be null, so we should not add this mandatory check (checkNotEmpty).

@MeteorSkyOne
Copy link
Contributor

Hi @taojintianxia can I take this on?

@MeteorSkyOne MeteorSkyOne linked a pull request Jan 19, 2025 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants