Amazon ec2 Java mysql - java

I am trying to setup an Amazon EC2 with tomcat and mysql. Both are up and running, both are in same instance. My confusions is, what jdbc url I have to use to connect my database on the same instance
<bean id="masterDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>WHAT TO ADD HERE</value>
</property>
.....

Add them like this:
<property name="url" value="jdbc:mysql://localhost/_dbName" />
<property name="username" value="your username" />
<property name="password" value="your password" />

Try the following:
jdbc:mysql://localhost/database_name?user=your_username&password=your_greatsqlpw
or
jdbc:mysql://127.0.0.1/database_name?user=your_username&password=your_greatsqlpw
As long as your server is secure you should not be concerned about security too much since the connections are only internal in the server.

Related

Spring FTP Client connection without TLS

I am stuggelin to create a ftp connection with the spring ftpSessionFactory.
In my project I am using the xml configuration for a ftp connection with TLS (it works):
<bean id="ftpSessionFactory"
class="org.springframework.integration.ftp.session.DefaultFtpsSessionFactory">
<property name="host" value="#{configurationService.configuration.getProperty('file.transfer.server.host')}" />
<property name="port" value="#{configurationService.configuration.getProperty('file.transfer.server.port')}" />
<property name="username" value="#{configurationService.configuration.getProperty('file.transfer.server.user')}" />
<property name="password" value="#{configurationService.configuration.getProperty('file.transfer.server.password')}" />
<property name="clientMode" value="2"/>
<property name="fileType" value="2"/>
<property name="useClientMode" value="true"/>
<property name="keyManager" ref="keyManager"/>
<property name="protocol" value="TLS"/>
<property name="trustManager" ref="trustManager"/>
<property name="prot" value="P"/>
<property name="needClientAuth" value="true"/>
<property name="sessionCreation" value="true"/>
<property name="implicit" value="false"/>
</bean>
Now I need a second connection, but without TLS (dont ask :D). For that I just replaced the fields in Java:
ftpSessionFactory.setHost(host);
ftpSessionFactory.setPort(port);
ftpSessionFactory.setUsername(username);
ftpSessionFactory.setPassword(password);
ftpSessionFactory.setProtocol(StringUtils.isNoneEmpty(protocol) ? protocol : null); // <-- null for no TLS
But that gives me this error: javax.net.ssl.SSLException: 500 AUTH: unknown command.
Then I tried it the hard coded way (it works):
FTPClient f = new FTPClient();
f.connect(host);
f.login(username, password);
Now my question:
How can I modify the xml part (I guess with setter) so it works for both?
Use DefaultFtpSessionFactory instead of DefaultFtpsSessionFactory.

Creating a Spring DataSource for connecting to Google Cloud SQL

I'm trying to find the best way to create a dataSource in Spring for connecting to a Google Cloud SQL instance.
I'm currently using:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.GoogleDriver" />
<property name="url" value="jdbc:google:mysql://myappid:instanceId/mydb?user=myuser" />
<property name="username" value="myuser" />
<property name="password" value="mypassword" />
</bean>
However, I'm a little concerned about using the DriverManagerDataSource provided by Spring as it's documentation says it creates a new connection for every call.
Before migrating over to App Engine I was using a connection pool called BoneCP - however it uses classes that are restricted by App Engine. Is there a connection pool or some other data source class that is recommended to be used with Google Cloud SQL?
Try c3p0 or commons-dbcp. They both implement javax.sql.Datasource which is whitelisted by app-engine.
Example on commons-dbcp:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.GoogleDriver" />
<property name="url" value="jdbc:google:mysql://myappid:instanceId/mydb?user=myuser" />
<property name="username" value="myuser" />
<property name="password" value="mypassword" />
<property name="validationQuery" value="SELECT 1"/>
</bean>

How can I locate an embedded h2 db based on my SQuirrel configuration?

I have a simple h2 database example, I assume it is a database that is stored in a single file. But where do I find this file? I'd like to connect to that db using SQL clients like Squirrel. Where is this file placed by default?
<property name="eclipselink.jdbc.platform"
value="org.eclipse.persistence.platform.database.H2Platform" />
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:~/myDB;FILE_LOCK=NO" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="sa" />
Based on the following value:
jdbc:h2:~/myDB;FILE_LOCK=NO"
It appears that your database file is located in your home directory in a file called myDB
The ~ denotes your home directory.
You can use the following code to run H2 in server mode and connect using SQuirrl SQL client.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.h2.Driver" />
<property name="url" value="jdbc:h2:target/h2/ps;AUTO_SERVER=TRUE" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
You can use SQuirrel SQL client (http://squirrel-sql.sourceforge.net/) to connect to you H2 database and look at the tables.
Create new connection.
Select H2 in the driver dropdown menu
Set url to your project target folder h2 folder (jdbc:h2:C:\projects\workspace\TestProject\target/h2/ps;AUTO_SERVER=true)
Enter user name ("sa")
Enter password ("")
In your example the file is placed in the file myDB under your home (represented as ~ ) directory :
<property name="javax.persistence.jdbc.url" value="jdbc:h2:**~/myDB**;FILE_LOCK=NO" />

Configure two PostgreSQL database(fail over mechanism) using Spring 3

I am using Tomcat JDBC API(org.apache.tomcat.jdbc.pool.DataSource) to connect to my PostgreSQL database from Spring configuration file as shown below. I got a new requirement to configure two databases which should act as a fail over mechanism, Like When one database is down application should automatically switch back to another database.
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost/dbname?user=postgres" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
<property name="maxActive" value="5" />
<property name="maxIdle" value="5" />
<property name="minIdle" value="2" />
<property name="initialSize" value="2" />
</bean>
Can anyone suggest how this can be achieved using Spring configuration file.
The normal way this is done is by using virtual IP addresses (with possible forwarding), checking for activity, a shoot-the-other-node-in-the-head approach and proper failover. Spring is exactly the wrong solution to this if you want to avoid things like data loss.
A few recommendations.
repmgr from 2ndquadrant will manage a lot of the process for you.
Use identical hardware and OS and streaming replication.
Use virtual IP addresses, and the like. Use a heartbeat mechanism to trigger failover via rempgr
Then from this perspective your spring app doesn't need reconfiguring.

ec2+tomcat+mysql Access Denied!

I'm running tomcat6 and mysql5 on a single ec2 instance and i cannot cannot from the outside world.
My context.xml on tomcat in ec2...
<bean id="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<property name="url" value="jdbc:mysql://ec2-xx-xx-xxx-178.compute1.amazonaws.com:3306/data_dbo" />
<property name="user" value="a_user" />
<property name="password" value="a_password" />
</bean>
Client context.xml
<bean id="myService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl" value="http://ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com:8080/MyService-services/remoting/thingServiceExporter"/>
<property name="serviceInterface" value="com.things.services.MyService"/>
</bean>
Error on clinet...
Caused by: java.sql.SQLException: Access denied for user ''#'domU-xx-xx-xx-xx-xx-DB.compute-1.internal' (using password: NO)
I've setup privileges for my user but it doesn't seem to be using the user i setup in my tomcat datasource. I say this because of the error on the client (Access denied for user ''#) no user, no password. Do i need to setup something between tomcat and mysql on ec2 because it seems to be using the internal dns to access MySql?
Let me know if i can provide any other information!
I found the issue. My persistance.xml had the following code...
<properties>
<property name="hibernate.connection.username" value=""/>
<property name="hibernate.connection.driver_class" value=""/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value=""/>
</properties>
Not sure how that got in there! The settings in the persistance.xml must override the settings in my Spring context file.

Categories