I am in need to clarify below ,
Is it mandatory to set the hibernate.hbm2ddl.auto property If we are using Hibernate.
hibernate.hbm2ddl.auto create-drop will affect any thing in the production DB
I am using spring localsession factory builder to build hibernate session .The queries all executed fine by using #Transaction but after query execution im getting Invalid data access sql grammer exception.I am assuming Hibernate trying to update something with DB which couldn't.
That's y asking help on hbm2ddl.auto property?
No. The default is fine: it does nothing with the database
Well, it will drop your database (you'll lose everything) when the session factory is closed, and recreate the schema when it restarts. So you really, really don't want that in production.
If you want help on your exception, you should show your code, show the stack trace of the exception, and show the relevant information (what the relevant tables look like, for example). I doubt hbm2ddl.auto has anything to do with that exception. Usually, when an exception occurs, it's a problem with your code, not a problem with hibernate. That's true of any framework/library.
Related
I have a web application which I've been slowly migrating from iBATIS 2 to JPA with Spring Data.
For the most part, things have been going well, with me just migrating the DAO for one domain object at a time. However, an issue that's been brought to my attention recently is that stale result lists are being show in some parts of the site.
For example, I have a "ticket" section, which shows a list of open tickets, and lets you view specific tickets on separate pages. When I create a new ticket, I can view that ticket on its specific page correctly. However, the open tickets list doesn't seem to show this new ticket until some time later.
Things I've tried to rule out:
I see this issue even on a system with MySQL's query cache disabled
I see this issue even when I set cacheModelsEnabled="false" in the iBATIS config.
I see this issue even when I completely remove the <cacheModel> element and cacheModel="x" attributes from my sqlMap file.
As soon as I restart the application, I see the up-to-date results.
When I execute the query iBATIS should be running here in a MySQL client, I do see the new ticket which is missing from iBATIS' results.
When I mocked up a simple ticket list using Spring MVC and Spring Data JPA, I do see the new ticket.
I've also tried to rule out some sort of weird transaction state with iBATIS, but it doesn't seem that any transaction is being used here at all.
What am I missing? Is there anything else I should be trying to figure this out? Or, should I just prioritize replacing the iBATIS layer completely with Spring Data JPA, which seems to be immune from this problem?
UPDATE
I've now gone through a lot of my recent changes with git bisect, and I've narrowed it down to a change that introduced Spring's org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.
So, it would seem that some transaction is living longer than it should. I'll add more logging to see if I can confirm this, and then look for a way to avoid using that filter.
If you are doing select, insert, select in the same SqlSession,
then the SqlSession cache is causing this issue. You will need to
clear the cache manually after the insert: sqlSession.clearCache().
So, it seems a combination of things ended up happening here:
Most of my code was not explicitly using transactions.
I had at some point changed to use Tomcat's JDBC Connection Pool, which does not reset autocommit by default when a connection is returned to the pool. I expect that my older DBCP-based stuff did this implicitly, though.
The introduction of OpenEntityManagerInViewFilter may have caused a SET autocommit=0 to be called at some point, with no corresponding SET autocommit=1 later, if nothing had changed.
By chance, or perhaps some design, the code that inserted a new record into the database and then immediately retrieved and showed it, seemed to get a different Connection than the code that showed my list of records.
The default MySQL transaction isolation level of REPEATABLE-READ meant that my listings were showing the old results.
The fix I've found, which seems to work in my testing so far, is to add these defaultAutoCommit and jdbcInterceptors attributes to my connection pool config:
<Resource name="jdbc/DB" auth="Container" type="javax.sql.DataSource"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
...
defaultAutoCommit="true" jdbcInterceptors="ConnectionState;StatementFinalizer" />
I'm trying to create database schema on application server startup.
I use hebernate 4.1.9 with annotations and hibernate.cfg.xml for configuration.
So the problem is that I cann't fully understand what I should do to create schema and after that use it in application. Of course I want to perform schema creation only on first start and on the next start I want to update it.
I'm trying to use hbn2ddl.auto in update state, but database doesn't creates. May be I should use somethin like INIT=create schema IF NOT EXISTS myschema in the end of the hibernate.connection.url??
Also I have an exception
org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
But in hibernate.cfg.xml there is such string:
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
Can anybody describe it to me?
I solve the problem by myself. I should add createDatabase=true to hibernate.connection.url
Coming from a mysql background, I am able to set the default schema name that I want to use for all my sql queries in the connection url. I now have an Oracle DB that I need to access. I am aware that I cannot specify the schema I want to use in the URL since the user is the schema name being used.
I realize that I can use a line of SQL code:
ALTER SESSION SET CURRENT_SCHEMA=default_schema
The project is using mybatis 2.3.5 as my SQL framework, but I am completely new to mybatis. Is there a simple way to configure mybatis to accomplish this? My application is a Spring 3 application, so I am using the Spring DataSourceTransactionManager to manage my transactions. I would presume that the manager must be made aware of this requirement to ensure that the command is sent whenever creating a new connection.
I've tried searching online, but most of the examples I find all have the schema names included within the sql queries in the SqlMaps, which I find to be bad practice.
In an ideal world, the schema name would be part of the URL such that I can make changes to the schema name for different environments (ex: dev, test, prod, etc) without touching the code (ie: only configured at the JNDI/application server level). I would be happy if I could use a Spring configuration value to set this as well as I could still use a JNDI lookup or a system environment property to retrieve the value.
Can anyone point me in the right direction?
Thanks,
Eric
As far as I know, there is no option in Oracle to change your URL in order to connect to a specific user schema.
1) mybatis: You may set your current schema to a deserved one before you start your operations. You can write your specification in a property file and set your method's arguments from that file. You do not need to change your code to change your schema in that case.
<update id="mySetSchemaMethod" parameterClass="String">
ALTER SESSION SET CURRENT_SCHEMA = ${schemaName}
</update>
2) trigger: If you are using this connection only for this particular java application, you can set a client event trigger so set your CURRENT_SCHEMA. This time, you need to change the trigger in order to manage test/prod changes.
CREATE OR REPLACE TRIGGER Set_Schema_On_Logon
AFTER LOGON
ON MY_SCHEMA
BEGIN
ALTER SESSION SET CURRENT_SCHEMA = MY_TEST_SCHEMA;
END;
I used to have a database called database and everything was working well using hibernate and its models.
I removed <property name="hibernate.hbm2ddl.auto"> to avoid update or create as it's a production server, we want to do it manually.
We recently switched to database2 and so we switched the hibernate configuration file and all the hibernate XML models.
`<class name="com.api.models.database.MmApplications" table="mm_applications" catalog="database2">`
but it keeps looking for database event if we migrated the database, the models and the connexion.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'database.mm_applications' doesn't exist
Does someone can help me ?
UPDATE ----
Hibernate is connecting to the right database (database2), but there is a prefix as a prefix database. making the queries hitting the database instead of database2, and when I try to force the default_schema my queries become :
`... from database.database2.mm_applications ....`
Any idea?
My database is specified in the hibernate.connection.url property. Have you changed that also ? An example would be: jdbc:mysql://localhost/mydatabase
Also, instead of removing hibernate.hbm2ddl.auto then perhaps you should set its value to validate. That way hibernate will ensure that the datamodel matches the database schema.
I found the problem, It was an other application deployed on the same tomcat server using hibernate as well with another database (database) making a conflict with the new application ...
There is still something weird, by connecting to any database, hibernate will use the specified catalog in the hibernate models and so constructing the request using the catalog.table_name
Hope this help someone someday.
I have a Java Web application using Hibernate and DB2 for iSeries and during update of a table I get he following error:-
Error SQL7008 while updating a DB2 for
iSeries table
From doing some googling on this error message I noticed that it happens when you are running an insert/update in a non-transactional mode. The explanation is given here.
This occurs because the table you are
trying to update is not being
journalled, and your update is being
run within a transaction.
Generally, you should always commit (and rollback if an exception occurs) your transactions. Usually I never set auto commit to true but in this case I would like to understand if it's truly needed as mentioned in the link above. Can you set the auto commit to true in your connection to see if this goes away?
<property name="hibernate.connection.autocommit" value="true"/>
Also this link has some tutorials on transaction management with hibernate.
I found the answer to my question,
This occurs As CoolBeans mentioned because the table I was trying to update is not being journalled.
Add this table to Journal, here are the steps
this took care of my problem.