MSSQL - Connection String - java

I have made a project using Entity Framework in Visual Studio. I have following connection string
<connectionStrings>
<add name="HospitalManagementEntities" connectionString="metadata=res://*/HospitalEntities.csdl|res://*/HospitalEntities.ssdl|res://*/HospitalEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=HospitalManagement;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="HospitalManagementContext" connectionString="metadata=res://*/HospitalManagement.csdl|res://*/HospitalManagement.ssdl|res://*/HospitalManagement.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=HospitalManagement;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
It connects with the mssql.
But when I try to connect with same database from Java - Hibernate/Spring it gives me error.
UDP Connection String
<bean id="dataSourceMain"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost\SQLEXPRESS;databaseName=HospitalManagement;integratedSecurity=true;Trusted_Connection=yes;" />
</bean>
Error
Could not obtain connection to query metadata : The connection to the
host localhost, named instance sqlexpress failed. Error:
"java.net.SocketTimeoutException: Receive timed out". Verify the
server and instance names and check that no firewall is blocking UDP
traffic to port 1434. For SQL Server 2005 or later, verify that the
SQL Server Browser Service is running on the host.
TCP Connection String
<bean id="dataSourceMain"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost;databaseName=HospitalManagement;integratedSecurity=true;Trusted_Connection=yes;" />
</bean>
Error
Could not obtain connection to query metadata : The TCP/IP connection
to the host localhost, port 1433 has failed. Error: "Connection
refused: connect. Verify the connection properties. Make sure that an
instance of SQL Server is running on the host and accepting TCP/IP
connections at the port. Make sure that TCP connections to the port
are not blocked by a firewall.".
Ports Info
Netstat
Questions
As you can see that both udp and tcp ports are blocked/inactive.
1) So how Visual Studio managed to get connected to MSSQL ?
2) What protocol does Entity framework uses in above case ?
3) Is there a way to connect to Mssql in Java - Hibernate/Spring, bypassing the firewall or udp/tcp requirements ?
P.S. I have no admin rights. So for now I can't enable TCP/IP port.

Related

Mq Connection error 2058

I have a spring core application with config below.
I'm using UserCredentialsConnectionFactoryAdapter, MQQueueConnectionFactory and jms-listener.
<jms:listener-container container-type="default"
connection-factory="userConnectionFactory" acknowledge="auto">
<jms:listener destination="${QUEUE_NAME_IN_GEN}" ref="messageListener"
method="onMessage" />
</jms:listener-container>
<bean id="userConnectionFactory"
class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory">
<ref bean="mqConnectionFactory" />
</property>
<property name="username" value="${MQ_USER_ID}" />
</bean>
<bean id="mqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="hostName">
<value>${MQ_HOST_NAME}</value>
</property>
<property name="port">
<value>${MQ_PORT}</value>
</property>
<property name="queueManager">
<value>${QUEUE_MANAGER}</value>
</property>
<property name="transportType">
<value>1</value>
</property>
</bean>
on application startup, listener stars perfectly one one machine.
when I try with same artifacts on a different server, listener fails to start with following error:
[org.springframework.jms.listener.DefaultMessageListenerContainer#0-1] ERROR org.springframework.jms.listener.DefaultMessageListenerContainer.refreshConnectionUntilSuccessful(DefaultMessageListenerContainer.java:909) - Could not refresh JMS Connection for destination 'R.ABCDEF' - retrying in 5000 ms. Cause: MQJMS2005: failed to create MQQueueManager for 'myhost:dev'; nested exception is com.ibm.mq.MQException: MQJE001: An MQException occurred: Completion Code 2, Reason 2058
MQJE036: Queue manager rejected connection attempt
To figure out if it's something to do with Unix account privilege issue on the second server, I wrote a simple MQ Client application. This program can connect to this queue manager and read messages from it.
What else could be wrong?
A 2058 suggests that the queue manager name is incorrect. According to the technote from IBM that's the most common cause, however there are others.
The following extract is taken from this technote:
Ensure that the queue manager name is specified correctly on:
MQCONN API calls
QREMOTE object definitions
Client connection channel definitions
Debugging QCF, TCF, or Client connection problems are much more complex.
Ensure that the connection request is routed to the intended machine and queue manager.
Verify that the listener program is starting the channel on the correct queue manager.
Ensure that the specifications for the client environment variables are correct.
mqserver
mqchllib
mqchltab
If you are using a client channel table (amqclchl.tab), then verify that your client connection channel definition has the correct queue manager name (QMNAME) specified.
Specify the correct queue manager name.
Correct channel routing problems.
Correct inetd listener configuration problems.
Correct client related configuration problems.

Not able to connect to Azure SQL Database server using spring

I am able to connect to database which is having ip address,but if
i have my database created in Azure SQL Database server in that i am not able to connect using spring configuration Java web application.It shows me error saying
The connection to the host vinayaka.cloudapp.net,555, named instance sqlexpress failed. Error: "java.net.UnknownHostException: vinayaka.cloudapp.net,555". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
spring-config.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://vinayaka.cloudapp.net,555\sqlexpress;database=Sample" />
<property name="username" value="user" />
<property name="password" value="pass" />
</bean>
Note:
1) I am able to open and work on that server in SQL Server Management Studio.
2) If I am able to open in SQL Server Management Studio, it means my machine IP address is allowed by firewall.
3) I can connect to database created in Azure SQL Database.
Please give suggestion on this.
The connection string of Azure SQL Database for JDBC is like jdbc:sqlserver://<hostname>.database.windows.net:1433;database=<dbname>;user=<user>#<hostname>;password={your_password_here};encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;.
You can check the connection string of your Azure SQL Database on Azure old portal or new portal, please see the pictures below.
Pic 1. Check the Connection String for JDBC on Azure old portal
Pic 2. Check the Connection String for JDBC on Azure new portal
Is 555 the port, that is exposed by the database for connection?
Shouldn't the connection string look like this?
<property name="url" value="jdbc:sqlserver://vinayaka.cloudapp.net:555\sqlexpress;database=Sample" />
I have replaced , in your code with :
Change your connection string like "jdbc:sqlserver://{HostName}.database.windows.net;database={databaseName};encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30".
And use sqljdbc4 jar file. Hope it should work.

Unable to connect to postgres SQL with spring and hibrenate

I have spring web application , that i am trying to connect with postgress.
Here is the db connection properties
datasource.driver=org.postgresql.Driver
datasource.url=jdbc:postgresql://localhost:5432/sampleDb
datasource.username=postgres
datasource.password=postgres
Here is the application context
<bean id="appDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${datasource.driver}</value>
</property>
<property name="url">
<value>${datasource.url}</value>
</property>
<property name="username">
<value>${datasource.username}</value>
</property>
<property name="password">
<value>${datasource.password}</value>
</property>
</bean>
When i starting application it get an error messege.
Cannot create PoolableConnectionFactory (Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Why would this happen?
I restored this database form another machine.I dont know what they written in that sql file. There is a code in that sql dump file.
REVOKE ALL ON TABLE workflow_type FROM PUBLIC;
REVOKE ALL ON TABLE workflow_type FROM postgres;
GRANT ALL ON TABLE workflow_type TO postgres;
GRANT SELECT,INSERT,DELETE,REFERENCES,UPDATE ON TABLE workflow_type TO user1;
Is this happend becouse of this query?
did you try to connect with psql under command line before? its just to test if you pg_hba.conf was fine.
if you can't connect with psql. Open you pg_hba.conf file and if you are on a local computer replace md5 by trust to have something like :
local all all trust
this permit everybody to connect to you postgres server.
retry connection with psql command line.
If its work, retry to launch your server to see if spring can now connect to the db fine.
Is your Postgres up and running on localhost:5432? Are you able to connect to the DB via command line (e.g. psql <dbname> -h localhost -U <username> -W)?

Hibernate connection error

I am using Hibernate 3.6.10, MySQL database in my Play(Play 1.2.5) application. I am getting this error everyday
01:05:02,304 ERROR ~ The last packet successfully received from the server was 39,593,644 milliseconds ago.
The last packet sent successfully to the server was 39,593,644 milliseconds ago. is longer than the server configured value of 'wait_timeout'.
You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
After restarting the server, the application runs propertly. But after few hours I am getting the same error. Currently I am not using connection pool in my application.
The above issue is solved when I add the following connection details to the hibernate-cfg.xml file
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
Add hibernate-c3p0.jar to the build path.

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed [duplicate]

This question already has answers here:
JDBC connection failed, error: TCP/IP connection to host failed
(9 answers)
Closed 1 year ago.
I keep on getting this error. I tried one solution from Stack Overflow (below), which did not work for me:
SQL Exception while connecting to SQL server
I tried allowing port 1433 in the Windows firewall by using a link, that did not work either. Refer to section "To open a port in the Windows firewall for TCP access"
http://msdn.microsoft.com/en-us/library/ms175043.aspx
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP
connection to the host localhost, port 1433 has failed. Error:
"Connection refused: connect. Verify the connection properties. Make
sure that an instance of SQL Server is running on the host and
accepting TCP/IP connections at the port. Make sure that TCP
connections to the port are not blocked by a firewall.
Please help me to fix this problem.
You need to go to
Start > Microsoft SQL Server > Configuration Tools > SQL Server Configuration Manager
When it opens, go to SQL Server Configuration Manager > SQL Server Network Configuration > Protocols for SQLExpress.
There you'll find the Protocol TCP/IP, if disabled then Enable it. Click on TCP/IP, you'll find its properties.
In these properties, remove all the TCP Dynamic Ports and add value of 1433 to all TCP ports, and restart your SQL Server Services > SQL Server
And it's done.
Steps
Open the Sql Server Configuration Manager (Start -> Programs ->
Microsoft SQL Server 2008 -> Configuration Tools)
Expand SQL Server Network Configuration -> [Your Server Instance]
Double click TCP/IP
Under Protocol:
Ensure Enabled is Yes
Under IP Addresses:
Scroll to the bottom and set the TCP Port under IPAll, (1433 by
default)
Find the IP address you want to connect to and set Enabled and Active
to Yes
To query database SQL Server with JDBC
Firstly, You need to find out the IP of your SQL Server and Enable TCP/IP and set TCP/IP port either. To do that, do these steps:
Open SQL Server Configuration Manager
Choose SQL Server Network Configuration -> Protocols for -> Right click on TCP/IP -> select Enable (if already enabled then move to next step) -> Right click to TCP/IP again -> Properties -> Choose tab IP Address -> In IPAll group: Clear value of TCP Dynamic Ports and set 1433 to TCP Port
-> Click OK
Note: Copy one of the IPs in the Properties table to do config later
Double click to SQL Server Services - > Right click to SQL Server (USER) ** -> Choose **Restart
Now, you already have the IP, config server as below:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://put.the.ip.here:1433;databaseName=<DB-Name>" />
<property name="username" value="sa" />
<property name="password" value="p#ssW0rd" />
</bean>
Note: Remove destroy-method="close" out of the bean
be sure tha in connectionUrl, your slash is oriented to left like this:
String connectionUrl = "jdbc:sqlserver://localhost**\**itis;databaseName=dbMediaStore;user=;password=";

Categories