Convert Connection to EntityManagerFactory or EntityManager - database

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.)

Related

Yii2 Giving ActiveRecord Model Database Details on Construction

I am construction a login system for Yii2 that can create the identity from either within a table in the Yii2 application or from data from an external database, the data could reside in one of many, many databases.
I know i could setup second, third, fourth databases in the apps config, but it doesn't fit the use case as, as the database belongs to a user who could change it, etc.
What i need to do it instantiate an ActiveRecord model passing it the database details of the database it should connect to, to query.
I have managed to very much confuse myself over this, in trying to work out the correct way to do it.
I know you can pass a config array to the constructor, but am unable to understand how i should do it, should i create and object of \yii\db\connection and pass it to the __construct?
If so how do i then perform the connection ?
I have searched all over for similar use case, but am unable to discover anything that helps, even a pointer would be great.
Many Thanks
Ok, so after so more head banging and searching in a different way, i have discovered a very simple and sensible approach, and the answer does come from SO.
Yii2 set db connection at runtime

Symfony 2: Build own DB access layer

I'm new here and on a research trip. We would like to use Symfony2 for a new project.
Now we have the problem that we need to use a company wide self-developed DB access layer.
We don't want to lose the whole Symfony/Doctrine layer for handling data.
Our idea is now to create or overwrite database access layer below Doctrine.
Something like creating a bridge (like PDO) between Doctrine and our DB access layer.
Has anyone an idea how we can do this or maybe someone has done similar things already and can provide us with an how-to?
Please no questions why. We just have to use the company access layer!
Thank you
http://forum.symfony-project.org/viewtopic.php?f=23&t=37637
I found the solution in Doctrine2 doc. It's pretty easy.
I needed for development and testing (frontend and console) about half a day
I've done it in 4 steps:
copy Doctrine\DBAL\Driver\OCI8 to a new folder.
rename files.
change functions to use own database access layer.
add in config file
doctrine:
dbal:
driver_class: /Path/To/Driver/Class
Doctrine has not much but a bit of information about Abstraction Layer:
Supporting Other Databases
If you believe this ยง of the doctrine documentation, you can see that you may use the Common package because it does not require the DBAL. Sadly, the same thing cannot be said of the ORM package. The best thing to do is probably to fork this package so that it supports your own DBAL.
Condolences for being obliged to use this DBAL.

Can I use the connection from Entity Framework or should I create a new one?

I have a SQLite Database that I access using System.Data.SQLite. The database is read into my Entity Framework Objectcontext. However, I also need direct access to the database in certain cases.
Can I / Should I use the same connection that my Entity Framework object context is using? I.e. ObjectContext.Connection?
If yes, how can I access it? Casting Entities.Connection to SQLiteConnection results in "Unable to cast object of type 'System.Data.EntityClient.EntityConnection' to type 'System.Data.SQLite.SQLiteConnection'."
If not, should I create a seperate connection, open it and keep it open for the application duration, or should I open and close the connection every time I want to access the database with it?
ObjectContext.Connection is an EntityConnection. You can get the store connection via:
var sc = ((EntityConnection)ObjectContext.Connection).StoreConnection;
You can cast that to the SQLiteConnection.
It's OK to use this connection if you need it.
But, as #Chris mentioned, you can just use ObjectContext.ExecuteStoreQuery, etc., if that will do for you.
If you're using the DBContext API you can actually execute a direct query by using this: aDBContext.Database.SqlQuery
I wouldn't be surprised if something similar exists for the ObjectContext API, but I barely used that one and thus can't tell you what it is.
Now if for some reason you can't (or don't want) to do that, I'd create a new connection for your custom queries. Put them in a Using and close it as soon as you're done with it.
Entity Framework has built in methods for executing raw SQL queries if that's what you need to do.

Decrypt (only) connection string section in Winforms app.config

Ok, I know this has been asked a thousand times before, but no conclusive solution has been derived...so here is another really silly question!
I have a Winforms 3.5 app and using LINQ to SQL, hence the Connection string is ALWAYS stored in "app.config" by default (and VS2008 will not accept any other way of storing this - Ive even tried overwriting this in the IDE-generated code). That being said, any person with a bit of computer know-how needs to just browse to the install directory of the app, look for the [appname].exe.config file, and open it to reveal the supposed secure username/password for accessing the database. Even if you opt to encrypt/decrypt this section, it is only done when the app is running - so Im assuming that when the app is closed, the connString section reverts to plain text....that can be once again read. (....how do you win?!?!)
Keeping in mind that Im using LINQ and generating a LinqDataContext, what I would like to know is this:
Can the connstring not be stored elsewhere besides the app.config? (perhaps like in a user setting file that can be modified after installation)
If the above is possible, can I not store a pre-encrypted connstring into the config file (that certainly wont be able to be read) and in my application, opt only to decrypt the connstring whenever the connection is opened?
If none of this is possible, I may have to revert to using traditional ADO.NET (seeing that I already have hundreds of stored procs for all CRUD operations) - at least that way I can have control over how and where the connstring is stored.
BTW - sorry if this is a juvenile/confusing question to ask, and if you feel that Im wrong with anything Im explaining please let me know.
Much thanks!
You can store a pre-encrypted conn-string in the app.config, but i guess it will be useless since, your application sooner or later will decrypt the conn-string (And the plain text will be availiable to anyone interested!)
So you should obtain an SSL cerificate too... (Not a self signed one)

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