there a lot of simillar questions but non of them solve my problem.
I use JPA with hibernate and want to save UTF-8 characters.
My maven dependencies are:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>3.6.0</version>
</dependency>
So I use JPA 2.1 and QueryDSL 3.6.0.
My persistance.xml is:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="thePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/newsDB?useUnicode=true&characterEncoding=UTF-8"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<!--Hibernate properties-->
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Then I use XAMPP MySQL and Apache
And add
port= 3306
[client]
default-character-set = utf8
[mysqld]
init-connect='SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci
[mysql]
default-character-set = utf8
My DB Collation is:
And my table:
When I turn on the logger I saw:
TRACE [main] (BasicBinder.java:81) - binding parameter [1] as [VARCHAR] - [Xelian]
TRACE [main] (BasicBinder.java:81) - binding parameter [2] as [VARCHAR] - [Описание]
TRACE [main] (BasicBinder.java:81) - binding parameter [3] as [CLOB] - [Content of the text .Чирипаха.]
TRACE [main] (BasicBinder.java:81) - binding parameter [4] as [BIGINT] - [112]
TRACE [main] (BasicBinder.java:81) - binding parameter [5] as [TIMESTAMP] - [Thu Jan 22 19:44:59 EET 2015]
TRACE [main] (BasicBinder.java:81) - binding parameter [6] as [VARCHAR] - [Title]
TRACE [main] (BasicBinder.java:81) - binding parameter [7] as [TIMESTAMP] - [Thu Jan 22 19:44:59 EET 2015]
TRACE [main] (BasicBinder.java:81) - binding parameter [8] as [BIGINT] - [0]
You can see that the values are strange Чирипаха.. And throught phpmyadmin I see:
But I when I edit it manually
You can see that my DB shows Cyrilic sumbols properlly. So I am wondering where is the problem.
So. I uninstall XAMPP an isntal MySQL Server on my machine. Then run MySQL command Line Client :
show variables like "%character%";show variables like "%collation%";
And everything seems to be OK. utf-8 every where. Then results in the DB was not encoded properlly, still strange symbols were there. When I edit them Cyrillic was inserted ok. SO I suspect the Hibernate persistance.xml. I edit it and put:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="thePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dnesdb?useUnicode=true&characterEncoding=UTF-8" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
</properties>
</persistence-unit>
</persistence>
And now UTF-8 (Cyrillic) is persisted OK.
!!! And do not forget BEFORE creating the DB to set java property:
-Dfile.encoding=UTF-8
If the tables is created and after that you set -Dfile.encoding=UTF-8, the problem will stay. So drop the table set the java encoding property, then recreate table again.
I think you have solved this problem but my advice might be useful to other users.
I had a similar problem. I tried to use many of the recommendations from stackoverflow but to not avail. The problem was in the encoding of my Project in my IDE(Intellij IDEA). My Project has windows-1251 encoding and my database has utf8 encoding. You need to change your project encoding in according to your database. In Intellij : Settings->Editor->File Encodings->Project Encoding.
Related
My jpa application was working fine (in fact, my 2 jpa applications) since I update my mac to Catalina (and restart it, of course).
Since then I got
org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : null
Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
...
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
But from mySQL Workbench I can access my databases with no problem.
I've tried to reinstall mysql, even using brew to do it. Nothing works.
I changed nothing on my persistence.xml file nor my code.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="MyProject" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myschema"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="MyPassword"/>
<property name="javax.persistence.schema-generation.database.action" value="update"/>
</properties>
</persistence-unit>
</persistence>
My mysql version is 8.0.19.
Thank you!
Next day update (28/4):
By now, I have:
1) Added this line to my persistence.xml:
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
As I got a new error...
Access denied for user 'root'#'localhost' (using password: YES)
2) Followed this instructions to set root password:
https://medium.com/#aungzanbaw/how-to-reset-root-user-password-in-mysql-8-0-a5c328d098a8
Now my new error is
Connection refused
Next next day update (29/4):
Today I stoped and started mysql from terminal:
sudo /usr/local/bin/mysql.server stop
sudo /usr/local/bin/mysql.server start
I then I got a new error
The server time zone value 'CEST' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
Let's continue my investigation!
Following this question: https://es.stackoverflow.com/questions/48935/configurar-zona-horaria-jdbc-driver-java/48946?newreg=494e1840d4404575a81dc4ec10200266
I modified persistence.xml:
So I got a
Table 'myschema.mytable' doesn't exist
error.
Finally I got it, not sure how... probably restarting from comand line.
Here my final persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="MyProject" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/myschema?serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="MyPassword"/>
<property name="javax.persistence.schema-generation.database.action" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect" />
</properties>
</persistence-unit>
</persistence>
Using MySQL8Dialect is necessary to be able to create the tables without that error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=MyISAM' at line 1
I'm developing a Play 2.3 Application with JPA/Hibernate and use a Heroku/ClearDB.com MySQL Database for this but after the application connects successfully to the DB I get following error:
[error] c.j.b.h.AbstractConnectionHook - Failed to acquire connection to jdbc:mysql://eu-cdbr-west-01.cleardb.com:3306/heroku_b7ea7b2d2972532 Sleeping for 1000ms and trying again. Attempts left: 10. Exception: null.Message:User 'badd25925bdd5f' has exceeded the 'max_user_connections' resource (current value: 10)
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: User 'badd25925bdd5f' has exceeded the 'max_user_connections' resource (current value: 10)
I tried almost everything I saw on other posts but nothing helped... I think it has to do with pooling...?
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://eu-cdbr-west-01.cleardb.com:3306/heroku_b7ea7b2d2972532?reconnect=true"/>
<property name="javax.persistence.jdbc.user" value="badd25925bdd5f"/>
<property name="javax.persistence.jdbc.password" value="XXX"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.c3p0.min_size" value="2"/>
<property name="hibernate.c3p0.max_size" value="9"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
</properties>
</persistence-unit>
</persistence>
application.conf (relevant part)
db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql://eu-cdbr-west-01.cleardb.com:3306/heroku_b7ea7b2d2972532"
db.default.user="badd25925bdd5f"
db.default.password="XXX"
jpa.default=defaultPersistenceUnit
Thanks a lot for your help!
I don't know about using Hibernate, but with Ebean the connection pool by default is managed by BoneCP. Try to configure it in your application.conf:
# Max connections = partitionCount * maxConnPerPartition
db.default.partitionCount=2
db.default.maxConnectionsPerPartition=5
or in your persistence.xml:
<property name="bonecp.partitionCount" value="2" />
<property name="bonecp.maxConnectionsPerPartition" value="5" />
The error says that you are using more connections then your plan .So you can
Either upgrade you db plans see https://addons.heroku.com/cleardb#ignite
or
You can try scaling down your aplication see https://devcenter.heroku.com/articles/scaling
Solution to this problem (for Play 2.3) was to add the play-hikaricp library to the application.
When I run my Hibernate application it shows flowing error:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at com.jwt.hibernate.SimpleTest.main(SimpleTest.java:23)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/javawebtutor
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 5 more
I would like to mention that mySQL server version is 5.5 and connector: mysql-connector-java-5.1.20-bin.jar
and this is my hibernate application's config file named: hibernate.cfg.xml
<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javawebtutor</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create </property>
<mapping resource="com/jwt/hibernate/student.hbm.xml" />
</session-factory>
</hibernate-configuration>
Guide me where is the problem. There is issue regarding connector version or MySQL version mismatch??
What IDE are you using? Try and add it to your classpath there and then deploy.
If it is a web app try putting to WEB-INF/lib
If it is maven app put the snippet inside your pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
I have a spring mvc project downloaded from the web and fully working. I'm using maven and maven tomcat plugin to manage dependencies and to run the webapp in the built-in tomcat. I'm trying to add mySql support in my project. Since i'm new to maven and maven tomcat plugin, I don't know hot to do this. Before i tried to add mysql, all was working and i was able to launch my web app simply executing a tomcat:run maven goal.
For now, when i execute tomcat:run i get a
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Here is what i've already done after some reading around the web:
I added dependencies for mysql driver (and Hibernate annotations too since i want to use it) in my pom.xml, and specified the dependency for tomcat plugin:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<mode>context</mode>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
</dependencies>
</plugin>
You can also notice a tag to specify to use a context.xml file. But I don't know where to put this file. I readed it should be generated automatically in tomcat/conf, but it's not present. So i added it manually with this content:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mkyongdb" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="30" maxWait="10000"
username="root" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mkyongdb"/>
</Context>
Then in web.xml, located in tomcat/conf i added:
<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/mkyongdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I placed the same content in src/main/webapp/META-INF/context.xml and in src/main/webapp/WEB-INF/web.xml
With all these configuration, the error mentioned above doesn't appears. But if i try to use hibernate adding
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mkyongdb" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name="dataSource" >
<ref bean="dataSource" />
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.hbm2ddl.auto" >create-drop</prop>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql" >true</prop>
</props>
</property>
<property name="annotatedClasses" >
<list>
<value>org.mose.grouporganizer.entity.AccelerometerFeatures</value>
</list>
</property>
</bean>
then i get the comunication link failure. What i'm missing?
If it's needed i can add the full stack trace.
If your application works fine and you want to use MySQL as your database then you should add MySQL driver in your pom.xml and change Hibernate configuration. That it.
First upgrade to last tomcat maven plugin which is now at Apache.
See http://tomcat.apache.org/maven-plugin-2.0/
Regarding the context use
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<contextFile>path to your context file</contextFile>
</configuration>
</plugin>
I'm trying to hook my Ja-sig CAS server (v3.5 running on Tomcat7) up to a MySQL database for user authentication. I basically have a table 'users' in the database storing username/password pairs that I want CAS to check against. However, I'm having difficulty even getting my current configuration to deploy.
This is an excerpt from pom.xml as it relates to database connectivity:
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22-bin</version>
<scope>provided</scope>
</dependency>
And here is where I try to setup the database connection in WEB-INF/deployerConfigContext.xml:
<bean
class="org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler">
<property name="tableUsers">
<value>users</value>
</property>
<property name="fieldUser">
<value>username</value>
</property>
<property name="fieldPassword">
<value>password</value>
</property>
<property name="passwordEncoder">
<bean
class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">
<constructor-arg value="MD5" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/cas_db</value>
</property>
<property name="username">
<value>cas_server</value>
</property>
<property name="password">
<value>pass</value>
</property>
</bean>
It builds perfectly fine with Maven, but when I try to deploy it with Tomcat it doesn't work. I haven't been able to find anything particularly informative in any of the tomcat logs. I'm wondering if there might be a problem with 'commons-dbcp', since when I comment that out and use a simple authentication handler in deployerConfigContext.xml, I'm able to deploy.
There seems to be little/poor documentation of this from my current web research. If anyone has any good resources they could recommend as well, it would be greatly appreciated.
I finally found a trace of errors in the tomcat log localhost.YYYY-MM-DD.log. As it turns out, I needed to add commons-pool:
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
<scope>provided</scope>
</dependency>
which commons-dbcp is dependent upon. Installing this with Maven did away with the missing class exception I was getting.
My next problem was that I had mistakenly defined my datasource bean in the list of authenticationHandlers in deployerConfigContext.xml, which led to a type conversion exception. Moving the bean out of the list tag did the trick.
On one of the official guides for CAS + JDBC Authentication (https://wiki.jasig.org/display/CASUM/Using+JDBC+for+Authentication), they comment on that:
Note: It is recommended commons-dbcp 1.2.1 is used with MySQL instead of the newer version. I found that new version (1.2.2) will cause a Socket write error in MySQL, after your CAS is idle for more that 8 hours, which is the time that MySQL will clean up all idle connections.
Your problem might be related to the version of the commons-dbcp. In my case, I have a configuration similar to yours with the difference on the commons-dbcp version, I'm using 1.4 (no problems)