永続化レイヤーとして休止状態を使用する Tomcat7 (jboss EWS 2.0) を使用して OpenShift からアプリを実行しています。アプリケーションと UTF-8 エンコーディングは localhost では正常に動作しますが、OpenShift にデプロイすると UTF-8 エンコーディングが壊れます。現在、漢字はすべて「????」になります。
OpenShift で MySQL 5.5 を使用しています。
私が使用した休止状態の構成。
PersistenceJPAConfig.java
@PropertySource({ "classpath:persistence.properties" })
public class PersistenceJPAConfig {
@Autowired
private Environment env;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "org.plantz.persistence.model" });
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("jdbc.url"));
dataSource.setUsername(env.getProperty("jdbc.user"));
dataSource.setPassword(env.getProperty("jdbc.pass"));
return dataSource;
}
final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.connection.charSet",
env.getProperty("hibernate.connection.charSet"));
hibernateProperties.setProperty("hibernate.connection.characterEncoding",
env.getProperty("hibernate.connection.characterEncoding"));
hibernateProperties.setProperty("hibernate.connection.useUnicode",
env.getProperty("hibernate.connection.useUnicode"));
hibernateProperties.setProperty("hibernate.hbm2ddl.auto",
env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect",
env.getProperty("hibernate.dialect"));
return hibernateProperties;
}
}
持続性.プロパティ
################### DataSource Configuration ##########################
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/${OPENSHIFT_APP_NAME}?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8
jdbc.user=${OPENSHIFT_MYSQL_DB_USERNAME}
jdbc.pass=${OPENSHIFT_MYSQL_DB_PASSWORD}
init-db=false
################### Hibernate Configuration ##########################
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=create-drop
hibernate.connection.charSet=utf8
hibernate.connection.characterEncoding=utf8
hibernate.connection.useUnicode=true
post_start_mysql の変更をプッシュすると、
action_hooksは正常に機能しました
#!/bin/bash
echo "********MySQL_config it's running."
/usr/bin/mysql -h $OPENSHIFT_MYSQL_DB_HOST -u $OPENSHIFT_MYSQL_DB_USERNAME \--password=$OPENSHIFT_MYSQL_DB_PASSWORD -P $OPENSHIFT_MYSQL_DB_PORT \-e 'SET NAMES "utf8"; SET CHARACTER SET utf8; SET GLOBAL character_set_client=utf8,character_set_results=utf8, character_set_connection=utf8, character_set_server=utf8, collation_server=utf8_general_ci, character_set_database=utf8;'
pre_start_jboss
echo "*******jboss_Config it's running."
export JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8"
注: すべての Bean が初期化されると、自動セットアップ DB
@Component
public class SetupDB_Data implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private UserRepository userRepository;
@Override
@Transactional
public void onApplicationEvent(ContextRefreshedEvent arg0) {
final User user = new User();
user.setUserName("管理員");
user.setPassword(passwordEncoder.encode("xxxXXX"));
user.setEmail("xxxx@xmail.com");
userRepository.save(user);
}
}
次のようなデータベースがあります。
mysql> SHOW VARIABLES LIKE '%character%';
+--------------------------+------------------------------------------------+
| Variable_name | Value |
+--------------------------+------------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /opt/rh/mysql55/root/usr/share/mysql/charsets/ |
+--------------------------+------------------------------------------------+