Change default h2Port in Corda

I can connect to h2 database using this default jdbc:h2:tcp://10.0.1.6:53062/node for a particular node. I wanted to change it to custom as 50002. So I addedd the port in build.gradle file and started the node in using customOverrides in Main file.

build.gradle file :

node {
    name "O=PartyA,L=London,C=GB"
    advertisedServices = []
    p2pPort 10108
    rpcPort 10109
    webPort 10110
    h2Port 50002
    cordapps = [
            "com.template:states-and-contracts:$version",
            "com.template:other-sources:$version",
            "net.corda:corda-finance:$corda_release_version"
    ]
    rpcUsers = [[ user: "user1", "password": "test", "permissions": ["ALL"]]]
}

Main File:

startNode(providedName = CordaX500Name("PartyA", "London", "GB"), rpcUsers = listOf(user),customOverrides = mapOf("h2Port" to 50002))

But still it I couldnt connect to 50002. After building this is the config file structure.

node.conf file:

 h2port=50002
myLegalName="O=PartyA,L=New York,C=US"
networkMapService {
    address="localhost:10102"
    legalName="O=Controller,L=London,C=GB"
}
p2pAddress="localhost:10108"
rpcAddress="localhost:10109"
rpcUsers=[
    {
        password=test
        permissions=[
            ALL
        ]
        user=user1
    }
]
webAddress="localhost:10110" 

I can connect to this port only when I run via command prompt. I am facing this problem when I run via intellij idea. Please help

To change the H2 database port and ensure your node connects to it both when running via IntelliJ IDEA and from the command line, follow these steps:


1. Validate Configuration in build.gradle

Your build.gradle configuration appears to be correct. You set h2Port to 50002, and this should be reflected in the generated node.conf file. Ensure no typos exist in h2Port.


2. Pass the Custom Configuration via IntelliJ

When running your node through IntelliJ, the customOverrides provided in your startNode call may not be taking effect as expected. Here’s how to fix it:

  1. Modify Your Main File: Ensure you’re passing customOverrides properly in your startNode invocation.
startNode(
    providedName = CordaX500Name("PartyA", "London", "GB"),
    rpcUsers = listOf(user),
    customOverrides = mapOf("h2port" to 50002)
)
  1. Override Database Configuration Explicitly: To ensure the custom H2 port is respected, you can also include the following:
val customOverrides = mapOf(
    "h2port" to 50002,
    "dataSourceProperties" to mapOf(
        "url" to "jdbc:h2:tcp://localhost:50002/node"
    )
)

startNode(
    providedName = CordaX500Name("PartyA", "London", "GB"),
    rpcUsers = listOf(user),
    customOverrides = customOverrides
)

3. Check IntelliJ Run Configuration

IntelliJ may not load the build.gradle configuration or use the node.conf file correctly. Do the following:

  1. Open your Run Configuration in IntelliJ.
  2. Ensure the working directory is set to the correct path where the node.conf file is located.
  3. Add the necessary JVM options to point to your configuration file explicitly. For example:
-Dconfig.file=path/to/node.conf

4. Verify H2 Database Connectivity

When your node starts, ensure the H2 database is accessible at the custom port 50002. You can test this using the H2 Console or a JDBC connection:

  • H2 Console Command:
java -cp h2*.jar org.h2.tools.Server -tcp -tcpAllowOthers -tcpPort 50002
  • JDBC URL:
jdbc:h2:tcp://localhost:50002/node

5. Troubleshoot Port Issues

  • Logs: Check the node startup logs in IntelliJ or the console to verify if the H2 server binds to port 50002. Look for messages like:
Starting H2 database on port 50002
  • Port Conflicts: Ensure no other service is using port 50002 by running:
netstat -an | grep 50002

6. Additional Notes

If none of the above resolves the issue:

  1. Gradle Build Step: Rebuild the project using:
./gradlew clean build
  1. IntelliJ Cache: Clear IntelliJ’s cache via File > Invalidate Caches / Restart.
  2. Compatibility Check: Verify the versions of Corda, Gradle, and IntelliJ align with your project setup.

By following these steps, your custom H2 port (50002) should work both in IntelliJ and via the command line. Let me know if you encounter further issues!