DBCP Connection Pool loginTimeout - database

According to the DBCP Document, BasicDataSource does not support setLoginTimeout(). My question is then how do I set a LoginTimeout for the creation of Connection objects? I know I can set maxWait on the pool, but my understanding is that that'll only be used for when the pool is exhausted and you're waiting for an existing connection to free up. It will not save me from the situation where a new connection needs to be created, but the connection/login into the DB hangs.
Any help is appreciated. Thanks.

Well there is always an option to add correct parameter to the URL. Depending on which DB you are using you can add one of the parameters in JDBC url.
Here is the link that confirms that BasicDataSource does not support loginTimeout
And at the bottom of this blog There is a table which lists URL parameters for connection timeouts.

#Sap is right, you might use the JDBC url in order to add connection properties, for example:
basicDataSource.setUrl("jdbc:postgresql://192.168.0.100:5432/postgres?connectTimeout=TIME_IN_SECONDS");
Where TIME_IN_SECONDS can be any intenger value.
Besides, we can use BasicDataSource#setConnectionProperties. As the parameter says:
The connection properties that will be sent to our JDBC driver when establishing new connections.
Format of the string must be [propertyName=property;]*
NOTE - The "user" and "password" properties will be passed explicitly, so they do not need to be included here.
So, it might be something like (and following the previous example):
basicDataSource.setUrl("jdbc:postgresql://192.168.0.100:5432/postgres");
basicDataSource.setConnectionProperties("connectTimeout=TIME_IN_SECONDS");
P.S:
You can add more properties using ; at the end of the property.

Related

Slow first NpgsqlConnection in YugabyteDB YSQL

[Question posted by a user on YugabyteDB Community Slack]
It seems that connecting to a cluster is “slow” the first time through NpgsqlConnection.
Is this normal PostgreSQL behaviour or is this related to YugabyteDB?
We are using the latest 6.0.0-preview7 nuget to use the multi host connecting string for load balancing.
According to Allow extensibility in type loading and database capabilities · Issue #1486 · npgsql/npgsql, extra calls are made to system tables to map types when the first connection is created. In order to avoid the initial call, you can set connStringBuilder.ServerCompatibilityMode = ServerCompatibilityMode.NoTypeLoading; on the connection string. When setting this property the first call is not made and the connection will be faster.

what does server= means in ldap query

I am parsing Exchange 2010 Admin Events (they are written to event viewer).
I need to verify via LDAP query that the parameters received are valid (this is some sort of validation i guess, its legacy code).
The ldap query contains subquery: "server=SOME_IPV6_ADDRESS"
I failed to find documentation regarding to server attribute, I was looking at https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx
At first, I thought the query fails due to fact that IPv6 is not supported, Now I see that server attribute is a mystery.
the complete ldap query syntax is:
(legacyexchangedn=*)(msexchrecipienttypedetails=*)(server=SOME_IPV6_I_DUNNO)(|(anr==someuser)(distinguishedname=someuser)(objectguid=someuser)(msexchimmutableid=someuser)(msexchimmutableid=someuser)(proxyAddresses=smtp:someuser)(userprincipalname=someuser)(msexchmailboxguid=someuser))
If I remove the (server=SOME_IPV6_I_DUNNO) part, the query works fine.
So what is the server=* used to ? maybe special exchange server attribute ?
Thanks
After digging all over, i found there is a class called server.
Description is located here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683948(v=vs.85).aspx

Close SQL connections cleanly when connection dropped

Just wondering if there is a way that I can close all SQL connections and commands in the cleanest possible way when a connection is lost in VB.NET (can also be in C#.NET).
What I'm trying to achieve:
Using the System.Net.NetworkInformation.NetworkAvailabilityChanged event, I'm monitoring whether the connection has been lost. If it has been lost, I'm disabling all user input - works great. Network comes back on, UI is enabled. (thumbs up)
Now, however, comes my predicament. If an SQL query is executing before the connection drops and then the network is lost, then the query returns a null value as expected, however if that was mid population of datatables/fields, then I get NullReferenceExceptions.
My question is:
Is there anyway to cleanly exit a sub after the connection has dropped? I've tried Application.ExitThread, but that doesn't seem to quite cut it. Do I need to put dropped connection handlers within my objects, so that when the connection is dropped, the respective object won't return or try to assign null data?
Any help is greatly appreciated. Not asking for plain code, need explanations if at all possible. Cheers.
If you follow the "using" best practice, like:
Using cn As New SqlConnection(connectionString)
...
End Using
Then the compiler will generate code that cleans up the connection when an exception is thrown.

Convert Connection to EntityManagerFactory or EntityManager

The question: Is there any way to get an EntityManagerFactory or EntityManager object when all you have is a java.sql.Connection object?
The explanation: We have a project specifically built to access our Oracle database. This is all pretty much legacy code and the database isn't exactly set up ORM friendly. We are trying to make any new tables and additions use Eclipselink though. The problem I am having is that this entire project was setup to pass around the connection object. So I won't have the url, username, or password for accessing the database with an EntityManagerFactory.
I have tried pulling the information from the connection with the metadata and all I can get seems to be the url and the user name. The password seems to not be there, I am assuming for security reasons.
It seems like the EntityManagerFactory and Connection are very similar objects so I was hoping to find a simple way to convert it but have only found the EM to Connection, not the Connection to EM(or EMF) and so I come seeking help, or at least a definitive no, it is not possible. Thanks!
This was answered in the comments section. So thought I'd post it as an answer for clarity and completeness. In case anyone happens by this question in the future.
"You can wrap the connection in an EMF/EM using native API, but without knowing anything about the persistence unit (defined in a persistence.xml), you won't get any value from it. You would need to create a persistence unit (with entities etc) and then load it to use your connection, though it would be better if you used a connection pool which you can pass as a property to JPA" http://onpersistence.blogspot.com/2008/04/eclipselink-and-datasources.html
Thanks Chris (from the comments.)

Why does an Entity Framework Connection require a metadata property?

I switched my DAL from using LINQ over to Entity Framework. Because my application connects to different databases depending on the current user, I need to dynamically create the DataContext at run time and pass in the appropriate connection string. However, when I tried to programatically create an Entity Framework connection using my old connection string, the connection failed. It complained that it didn't recognize the key in the connection string, "server" to be exact.
I found out that I needed to do this in order to get the Entity Framework connection to work:
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = clientConnectionString;
entityBuilder.Metadata = "res://*/xxxxxxxxxx.csdl...";
Entities entities = new Entities(entityBuilder.ToString());
Why is this?
What is the Metadata property for?
Is it going to be a problem that its always the same for multiple different connections?
What should it be?
Is there any way around this?
Thanks in advance!
Update 1:
Thanks for the update Randolpho, but...
The whole reason I'm having this issue, is that I can't store the connection strings in a configuration file. The connection string is dynamically determined at runtime by which user is connecting.
Here is my exact scenario:
If user A is connecting, the app pulls data from database A. If user B is connecting, the app pulls data from database B.
The connection strings are stored in a main database, and the number is potentially limitless. Every time I add a user, I don't want to have to go into the web.config, not to mention the fact that it would eventually get HUGE!
Expanding on Randolpho's answer:
The metadata property specifically points to the location of the .SSDL (Storage Model,) .CSDL (Conceptual Model,) and .MSL (Mapping Model) files. These three files essentially are the Entity Data Model. The "res://" URI-style qualifier indicates that the files are embedded as resources in the compiled EDM assembly.
You will find these links very informative:
http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnection.connectionstring.aspx
http://weblogs.asp.net/pgielens/archive/2006/08/21/ADO.NET-Entity-Framework-Metadata.aspx
Bottom line? Entity Framework needs the metadata to build your entity mappings.
Additionally, you should consider moving your connection information out to your configuration file rather than build it in code. The first link will show you how to do that.

Resources