R
Railway13mo ago
bonjr

Deployment and logging issues (Maven)

Hello, My program is running fine locally, but when I deploy it with Railway, I start seeing issues. I think the issue is primarily with grabbing the environment or database variable.
private static Connection getConnection() throws SQLException {
// String dbUrl = toJdbcUrl(readFromFile("DATABASE_URL")); // local testing
String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL")); // live

return DriverManager.getConnection(dbUrl);
}

private static String toJdbcUrl(String databaseUrl) {
if (databaseUrl == null || !databaseUrl.startsWith("postgresql://")) {
return null;
}

int protocolEnd = databaseUrl.indexOf("://");
int credentialsEnd = databaseUrl.lastIndexOf("@");

String credentials = databaseUrl.substring(protocolEnd + 3, credentialsEnd);
String[] splitCredentials = credentials.split(":");

String user = splitCredentials[0];
String password = splitCredentials[1];

String afterCredentials = databaseUrl.substring(credentialsEnd + 1);
String host = afterCredentials.substring(0, afterCredentials.indexOf(":"));
String portAndDatabase = afterCredentials.substring(afterCredentials.indexOf(":") + 1);

String jdbcUrl = "jdbc:postgresql://" + host + ":" + portAndDatabase + "?user=" + user + "&password="
+ password;

return jdbcUrl;
}
private static Connection getConnection() throws SQLException {
// String dbUrl = toJdbcUrl(readFromFile("DATABASE_URL")); // local testing
String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL")); // live

return DriverManager.getConnection(dbUrl);
}

private static String toJdbcUrl(String databaseUrl) {
if (databaseUrl == null || !databaseUrl.startsWith("postgresql://")) {
return null;
}

int protocolEnd = databaseUrl.indexOf("://");
int credentialsEnd = databaseUrl.lastIndexOf("@");

String credentials = databaseUrl.substring(protocolEnd + 3, credentialsEnd);
String[] splitCredentials = credentials.split(":");

String user = splitCredentials[0];
String password = splitCredentials[1];

String afterCredentials = databaseUrl.substring(credentialsEnd + 1);
String host = afterCredentials.substring(0, afterCredentials.indexOf(":"));
String portAndDatabase = afterCredentials.substring(afterCredentials.indexOf(":") + 1);

String jdbcUrl = "jdbc:postgresql://" + host + ":" + portAndDatabase + "?user=" + user + "&password="
+ password;

return jdbcUrl;
}
The reason I think it's some database/environment issue is because the logs on Railway says:
Exception in thread "main" javax.security.auth.login.LoginException: The provided token is invalid!

at net.dv8tion.jda.internal.JDAImpl.verifyToken(JDAImpl.java:362)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:279)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:246)

at net.dv8tion.jda.api.JDABuilder.build(JDABuilder.java:1918)

at Main.main(Main.java:57)
Exception in thread "main" javax.security.auth.login.LoginException: The provided token is invalid!

at net.dv8tion.jda.internal.JDAImpl.verifyToken(JDAImpl.java:362)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:279)

at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:246)

at net.dv8tion.jda.api.JDABuilder.build(JDABuilder.java:1918)

at Main.main(Main.java:57)
So it would appear that this part from main function around the JDA part is having issues:
// bot
JDABuilder builder = JDABuilder.createDefault(Utility.readFromDatabase("TOKEN"));
// bot
JDABuilder builder = JDABuilder.createDefault(Utility.readFromDatabase("TOKEN"));
I have double checked that the TOKEN variable is the same locally and on the Postgres database, so the issue is not the token itself.
public static String readFromDatabase(String key) {
String sql = "SELECT value FROM config WHERE key = ?";

try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, key);

ResultSet rs = stmt.executeQuery();

if (rs.next()) {
return rs.getString("value");
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}

return null;
}
public static String readFromDatabase(String key) {
String sql = "SELECT value FROM config WHERE key = ?";

try (Connection conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(sql)) {

stmt.setString(1, key);

ResultSet rs = stmt.executeQuery();

if (rs.next()) {
return rs.getString("value");
}
} catch (SQLException e) {
System.out.println("Error: " + e.getMessage());
}

return null;
}
I can't seem to figure out what is wrong. And also, how can I use the deploy log to my advantage for debugging? Locally, sometimes i'll just do System.out.println to check variables, etc., but this does not show up in the deploy logs.
72 Replies
Percy
Percy13mo ago
Project ID: e202dece-bdd8-491e-810f-9d28bf4c36e6
bonjr
bonjr13mo ago
e202dece-bdd8-491e-810f-9d28bf4c36e6 Just realized I can use
private static Connection getConnection() throws SQLException {
String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL"));

return DriverManager.getConnection(dbUrl);
}
private static Connection getConnection() throws SQLException {
String dbUrl = toJdbcUrl(System.getenv("DATABASE_URL"));

return DriverManager.getConnection(dbUrl);
}
for both dev and production, and once again, everything works fine locally, so something is going on with System.getenv on Railway
Brody
Brody13mo ago
show us your service variables please
bonjr
bonjr13mo ago
mullet.
mullet.13mo ago
Are you Sure this is a correct url? On some line in your code you check if the string starts with postgres:// Last I checked the one on your picture doesn't seem like it starts with that.
bonjr
bonjr13mo ago
my undersatanding is that System.getenv("DATABASE_URL") returns ${{Postgres.DATABASE_URL}}, which is pointing to this no?
bonjr
bonjr13mo ago
bonjr
bonjr13mo ago
and given this, it does indeed start with postgresql:// unless my understnding is wrong
mullet.
mullet.13mo ago
It seems like you have double postgres:// in the beginning but it might be wrong from my part Oh you have a worker and then a seperate service where you define the database. I think you might need to use shared variables?
bonjr
bonjr13mo ago
interesting, could you explain this? it's my first time on railway
mullet.
mullet.13mo ago
Scratch that. Seems like it would still work the way you have it. https://docs.railway.app/develop/variables#reference-variables
Railway Docs
Variables | Railway Docs
Documentation for Railway
ThallesComH
ThallesComH13mo ago
hi, did you make sure you can connect to your databse and that it's returning something? If it was the environment variable, Java would thrown a connection exception. You could also set your token in an environment variable to make sure that it's a database connection issue
bonjr
bonjr13mo ago
All I can say for sure is that when I run it locally, without changing the code at all, it all works fine. So it seems that it is possible to connect to the database and retrieve/set things as needed. But once I deploy it to Railway, that's when the ssues pop up i was just thinking abt trying this
ThallesComH
ThallesComH13mo ago
also, instead of parsing the connection string, you can separately set the credentials, make the code a lot cleaner.
Brody
Brody13mo ago
you are close, that is called a reference variable, it references the postgres plugins own DATABASE_URL variable, but while you see ${{Postgres.DATABASE_URL}} in the railway website, railway will replace that with the DATABASE_URL connection string from the database plugin when running your app do a console log of this please System.getenv("DATABASE_URL") however, you said you logs are not making it into the deployment logs, so make sure you are logging to stdout
Want results from more Discord servers?
Add your server