Java JPA Load balancing read queries to Read MySql DB slave - java

I noticed that my Play Framework application is not sending the read queries to the read-only MySql slave.
I am using
com.mysql.cj.jdbc.Driver as javax.persistence.jdbc.driver.
jdbc:mysql:replication://write-db-url,read-db-url/db_name as javax.persistence.jdbc.url
The db's are AWS aurora MySQL-compatible with multi-az replica.
I am using hibernate as ORM.
I am using play framework.
Am I missing any configuration/code?

Everything else looks good as stated in question, like jdbc driver and url.
Because in your question very less information is provided related to ORM or JPA and connection codes that you are using.
I'm here by providing a simple main program that you could use debug your issue. Once that is done, focus on your app to see, are you missing same thing.
Here is how JDBC driver determines, whether to connect master or read replica.
If connection mode is read+write, which is default, then it goes to master.
If connection mode is read, then it goes to one of read-replica.
Here officially documentation.
import java.sql.Connection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Properties;
import java.sql.DriverManager;
public class ReplicationDemo {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
// We want this for failover on the slaves
props.put("autoReconnect", "true");
// We want to load balance between the slaves
props.put("roundRobinLoadBalance", "true");
props.put("user", "foo");
props.put("password", "password");
//
// Looks like a normal MySQL JDBC url, with a
// comma-separated list of hosts, the first
// being the 'master', the rest being any number
// of slaves that the driver will load balance against
//
Connection conn =
DriverManager.getConnection("jdbc:mysql:replication://master,slave1,slave2,slave3/test",
props);
//
// Perform read/write work on the master
// by setting the read-only flag to "false"
//
conn.setReadOnly(false);
conn.setAutoCommit(false);
conn.createStatement().executeUpdate("UPDATE some_table ....");
conn.commit();
//
// Now, do a query from a slave, the driver automatically picks one
// from the list
//
conn.setReadOnly(true);
ResultSet rs =
conn.createStatement().executeQuery("SELECT a,b FROM alt_table");
.......
}
}
Hope it helps.

Related

How do I connect to a remote Cassandra instance running in Docker to retrieve data using Java?

I'm trying to connect to a remote machine which has docker installed. I want to exec into the docker container and connect to database and then fetch a table data using java.
Following are my commands that i'm trying to execute.
docker exec it containerID - to login to docker container
cqlsh -u username -p password -- to connect to cassandra DB
use keyspace; ---to connect to cassandra keyspace
desc tables; --- to view the tables that are available in keyspace.
Following is the Java code that I'm trying. Can someone let me know if this approach is correct or what should i do to make this code work. I'm completely new to java code.
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class ExecuteanyCommands {
public static void main(String[] args) throws JSchException, InterruptedException, IOException {
//JSch
JSch jsch = new JSch();
Session session = jsch.getSession("cambi", "10.10.96.20", 22);
session.setPassword("axone");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("exec");
InputStream in = channel.getInputStream();
((ChannelExec) channel).setCommand("docker exec -it containerID; cqlsh -u username -p password; use keyspace; desc tables;");
channel.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while((line = reader.readLine()) != null) {
System.out.println(line);
}
channel.disconnect();
session.disconnect();
}
}
If you want to connect to a container from a remote machine over SSH, then the way to do it is to define an SSH tunnel, external to your Java program.
In addition, the idea of interacting with Cassandra by running cqlsh on the host is very unusual.
It's better to use the official Cassandra Java driver for that purpose.
To summarize:
Create an SSH tunnel between the client and the docker host
Write a Java program that uses the Java Driver to query Cassandra
I echo Jonathan Jacobson's point that your method isn't the correct. Running cqlsh which itself is an app/client to retrieve data from your Cassandra tables is incorrect.
You need to instead use one of the available Cassandra drivers to connect to your cluster. In your case, it would be the Java driver for Apache Cassandra.
On the Astra DB docs site, there's a fully working Java code example for connecting to a cluster. Here's a stripped down version that you can use:
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
public class ConnectDatabase {
public static void main(String[] args) {
try (CqlSession session = CqlSession.builder()
.withAuthCredentials("your_username","your_password")
.build()) {
// select the release_version from the system.local table
ResultSet rs = session.execute("SELECT release_version FROM system.local");
Row row = rs.one();
// print the results to the console
if (row != null) {
System.out.println(row.getString("release_version"));
} else {
System.out.println("An error occurred.");
}
}
System.exit(0);
}
}
You'll need to get a copy of the example pom.xml file to compile the app. You can get more information about the Java driver here.
Note that you will need to expose the ports in Docker so that Cassandra is accessible from outside the container. For example, map the CQL port with -p 9042:9042.
If you're new to Cassandra, I recommend having a look at datastax.com/dev which has lots of free hands-on interactive learning resources. In particular, the Cassandra Fundamentals learning series lets you learn the basic concepts quickly.
For what it's worth, you can also use the Stargate.io data platform. It allows you to connect to a Cassandra cluster using APIs you're already familiar with. It is fully open-source so it's free to use. Here are links to the Stargate tutorials on datastax.com/dev: REST API, Document API, GraphQL API, and more recently gRPC API.
Finally, we also run live workshops for free to teach developers how to build apps for Cassandra. We provide all the materials including all the source code for fully working apps plus coding environments on the cloud so there's nothing for you to install. There are 2-3 workshops every week and details are at datastax.com/workshops. Cheers!

Cant connect to mariadb from java - only with mysqldeveloper [duplicate]

I am trying to connect to a database in Mariadb through a simple java application but the connection is told to be unsuccessful and an Exception is thrown. I have done the similar connection using mysql and it was working correctly. The problem is maybe with the driver here.
try{
Class.forName("org.mariadb.jdbc.Driver");
Connection connection = DriverManager.getConnection(
"jdbc:mariadb://localhost:3306/project", "root", "");
Statement statement = connection.createStatement();
String uname="xyz",pass="abc";
statement.executeUpdate("insert into user values('"+uname+"','"+pass+"')");}//end of try block
I looked up the internet for the help and came by that driver class provided by the MariaDB Client Library for Java Applications is not com.mysql.jdbc.Driver but org.mariadb.jdbc.Driver! I changed it accordingly but it seems the problem is with the very first line inside the try block. The driver is not loading at all.
Also, I have added the mysql jar file to the libraries of my java application as in the screen-shot below. Please help me through this.
It appears that you are trying to use jdbc:mariadb://... to establish a connection to a MariaDB server instance using the MySQL JDBC Driver. That probably won't work because the MySQL JDBC Driver would use jdbc:mysql://..., regardless of whether it is connecting to a MySQL server or a MariaDB server. That is, the connection string must match the driver that is being used (rather than the database server being accessed).
The MySQL and MariaDB drivers are supposed to be somewhat interchangeable, but it only seems prudent to use the MariaDB connector when accessing a MariaDB server. For what it's worth, the combination of mariadb-java-client-1.1.7.jar
and
Connection con = DriverManager.getConnection(
"jdbc:mariadb://localhost/project",
"root",
"whatever");
worked for me. I downloaded the MariaDB Client Library for Java from here:
https://downloads.mariadb.org/client-java/1.1.7/
which I arrived at via
https://downloads.mariadb.org/
Additional notes:
There is no need for a Class.forName() statement in your Java code.
The default configuration for MariaDB under Mageia may include the skip-networking directive in /etc/my.cnf. You will need to remove (or comment out) that directive if you want to connect to the database via JDBC because JDBC connections always look like "network" connections to MySQL/MariaDB, even if they are connections from localhost. (You may need to tweak the bind-address value to something like 0.0.0.0 as well.)
An additional note:
Exploring the MariaDB JDBC driver, I found this inside the url parsing file:
Project: https://github.com/MariaDB/mariadb-connector-j.git
File: src/main/java/org/mariadb/jdbc/UrlParser.java
public static UrlParser parse(final String url, Properties prop) throws SQLException {
....
if (url.startsWith("jdbc:mysql:")) {
UrlParser urlParser = new UrlParser();
parseInternal(urlParser, url, prop);
return urlParser;
} else {
if (url.startsWith("jdbc:mariadb:")) {
UrlParser urlParser = new UrlParser();
parseInternal(urlParser, "jdbc:mysql:" + url.substring(13), prop);
return urlParser;
}
}
As you can see, the string "jdbc:mariadb:" is always replaced with "jdbc:mysql:" internally. So when it comes to the MariaDB driver, whether it is :mariadb: or :mysql: it always gets parsed as "jdbc:mysql:".
No difference.
if (url.startsWith("jdbc:mariadb:")) {
....
parseInternal(urlParser, "jdbc:mysql:" + url.substring(13), prop);
....

Java JDBC cannot connect to MSSQL Server

I tried many ways to solve my issue, none of them worked, so here i am with the question. I created a local database with SQL Server Management Studio. It's name is CallCenter, i created a user account for it, granted every privileges and i can log into the DB with it, in the Managemenet Studio, everything works just fine here.
Now i use NetBeans, to create the connection. I downloaded the Microsoft JDBC driver, set up everything, the JDBC seems to be working fine. The problem is, it cannot connect to the Database. I set the log in options to both Windows & SQL. I tried to log in, with integrated sequrity ( windows account ) aswell as the created ( and working ) SQL user account.
None of them worked, i keep getting this exception:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user
'admin'. ClientConnectionId:1ce0b951-5ecb-49b4-a4d0-ff4a96af4ed2 at
com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)......
Here is the code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Try {
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1434;databaseName=CallCenter;integratedSecurity=true;";
Connection conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
System.err.println("SQL Driver class does not exist!");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
I've been browsing the internet for hours, tried many solutions, but none of solved this problem for me. Please help me out here!
The Exception pretty much says it all - check your credentials (which you obviously have not given) with the database.
The Login failed for user 'admin' error which you get is a clear indication
that your login request reaches the SQL server but your credentials are wrong.
So either your username or your password is wrong, or maybe this login is not
configured for remote logins to the SQL server. Check your configuration on the server.
Use the other method of the DriverManager to get the connection using the Credentials.
When you have integratedSecurity=True, read this to get more idea on what it actually mean. It uses your windows credentials for login(With SQL Server).
So, make that false and provide credentials for the DB.
For your reference read this
[EDIT]
Try using this instead of integratedSecurity.
jdbc:sqlserver://HOSP_SQL1.company.com;user=name;password=abcdefg;database=Test

Can't start sql db in 11g through jdbc.Getting java sql exception

import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.OracleConnection;
public class Test {
static final String url2= "jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP
(HOST=192.168.1.171)(PORT=5521))"+"(CONNECT_DATA =(SERVICE_NAME = rdbms)))";
public static void main(String[] args) throws SQLException {
OracleDataSource ds = new OracleDataSource();
Properties prop = new Properties();
prop.setProperty("user","system");
prop.setProperty("password","manager");
prop.setProperty("internal_logon","sysdba");
prop.setProperty("prelim_auth","true");
ds.setConnectionProperties(prop);
ds.setURL(url2);
OracleConnection conn = (OracleConnection)ds.getConnection();
conn.startup(OracleConnection.DatabaseStartupMode.NO_RESTRICTION);
conn.close();
}
}
getting
Exception in thread "main" java.sql.SQLException: Listener refused the connection with the following error:
ORA-12514, TNS:listener does not currently know of service requested in connect descriptor
Can anyone help with this?
Cause:
The listener received a request to establish a connection to a database or other service. The connect descriptor received by the listener specified a service name for a service (usually a database service) that either has not yet dynamically registered with the listener or has not been statically configured for the listener. This may be a temporary condition such as after the listener has started, but before the database instance has registered with the listener.
Action:
- Wait a moment and try to connect a second time.
Check which services are currently known by the listener by executing: lsnrctl services
Check that the SERVICE_NAME parameter in the connect descriptor of the net service name used specifies a service known by the listener.
If an easy connect naming connect identifier was used, check that the service name specified is a service known by the listener.
Check for an event in the listener.log file.
You must understand that when it comes to exceptions, many times JDBC simply serves as a wrapper to errors that occur at the DB side,
or propagates driver specific (i.e - postgresql jdbc driver, oracle jdbc driver) specific errors.
You should google the error and you will find what I believe is a clear explanation:
http://ora-12514.ora-code.com/.
If you see one of the suggestions there is to retry after a few seconds and reconnect again.

Java: Trouble connecting to MySQL

I'm writing a desktop java app on that I want to connect to a MySQL database on a server. Here is the code to do that:
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
private static Connection getDBConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
String username = "myUserName";
String password = "myPassWord";
String url = "jdbc:mysql://www.domainName.com:3306/databaseName";
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Connecting to database...");
//hangs here
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
When I run this, it hangs on the DriverManager.getConnection() call. Why does this happen? Is my URL malformed?
(I'm not getting any error messages, but the program doesn't respond as if in an infinite loop. I haven't waited longer than 90 seconds to see if the connection will ever be established.)
Also, what is the purpose of the Class.forName() call? How does it work?
I am almost entirely certain that the username and password are correct. (I just used userName and passWord as placeholders above.)
UPDATE: I fixed the port number, and now I get this error:
Cannot connect to database: java.sql.SQLException: Access denied
for user 'userName'#'r236059121.resnet.mySchool.edu' (using
password: YES)
Does this mean I need to configure settings on the database? Or does it mean that I've got the credentials wrong? (They work for PHP scripts deployed on the server that contains the database.)
SOLUTION: Added the host above to the Access Host list on cPanel.
Seems to me like your database is not reachable and you will probably get an error when the call runs into a timeout. Are you sure the hostname and port are right and reachable from your machine?
You don't need the newInstance() at the end of Class.forName(). Class.forName() triggers the classloader to load that class, which in turn triggers some internal registration code in the driver which makes the driver available.
I think the line should just be
Class.forName("com.mysql.jdbc.Driver");
(close the .newInstance() bit)
That causes the driver to register itself with the driver manager and allows the driver manager to pick a driver for the database url.
I think the hang is caused by a DNS problem, or some other reason why your db cannot be reached. By default, the MySQL JDBC driver does not time out for a connection. See http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html and look for connectTimeout.
In your code, you have
String url = "jdbc:mysql://www.domainName.com:portNumber/databaseName";
I take it that you used a real port there? By default, it should be 3306. You can test with the test database which is present in virtually all mysql instances:
String url = "jdbc:mysql://www.domainName.com:3306/test";
You also wrote:
String username = "myUserName";
String password = "myPassWord";
Obviously you should use real credentials here too. Ask your dba what they are. If you're the DBA then...well you should probably read up on MySQl administration :) Seriously when you installed MySQL you were probably promted for a password for the root user. Use those (in the obvious way)
In real code you should probably not hang when the db is not there. So I advise adding a connectTimeout option like so:
String url = "jdbc:mysql://www.domainName.com:3306/test?connectTimeout=3000";
(connectTimeout is in milliseconds, so this would time out after 3 seconds)
Rosarch - You are not able to connect to your DB since its unreachable.
It'll timeout after a while.
Try telnetting -
telnet <IP-OF-domainName.com> <PortNumber>
You'll mostly see that it shows timeout.
Solutions -
1.) If you are behind a firewall, you need to punch a hole to allow access
2.) If you are behind a proxy, need to configure it to allow access

Categories

Resources